encountered an interview question, usually after an asynchronous operation error will enter. Catch (), now has to enter after three errors, how to achieve this.
encountered an interview question, usually after an asynchronous operation error will enter. Catch (), now has to enter after three errors, how to achieve this.
first of all, this topic is problematic. Once the Promise
state is determined, it will not change, so there is no point in handling multiple errors with the same Promise
.
the meaning of the comment is actually ajax
request 3 times, so give me a chestnut
let Ajax = function(option) {
console.log('ajax')
return new Promise((resolve, reject) => {
setTimeout(() => {
reject('some error ')
// resolve()
}, 1000)
})
}
function runAjax(option, retry = 2) {
let p = Ajax(option);
if (retry > 0) {
retry--;
return new Promise((resolve, reject) => {
p.then(resolve).catch(() => {
resolve(runAjax(option, retry))
})
})
}
return p;
}
runAjax({ hello: 'word' }).then(() => {
console.log('success')
}).catch((err) => {
console.log('run error', err)
})
can I say I don't understand? Is that what the interview questions are like?
as soon as I came into contact with promise, I wrote a demo by myself. Ask . Why my then only executes once, and how to make it execute multiple times var test = 0; var promise = new Promise(resolve => { $( .btn ).on( click ,()=...
a () { return new Promise((res, rej) => { : getData().then(res => { this.data = res.data.reslut console.log(this.data) }) }); } the a method is defined in vue , and the a method is ca...
write a chained call as follows: new Man( lan ).sleep(3).eat( apple ).sleep(3).eat( banana ); output: hello, lan--> (pause 3s)-> lan eat apple-> (pause 3s)-> lan eat banana my code is as follows: class Man { constructor(name) { th...
I have reviewed articles of various promise implementations, all of which indicate that it is necessary to maintain a _ deferreds to save the callback function set information registered by the then method, and some implementations maintain onResolve...
at present, I have written the login operation in app.js. After obtaining Wechat s code, I obtain the authentication access-token from my own server. the code is as follows app.js onLaunch: function () { wx.login({ success: ...
functiong getPromise(){ return Promise.resolve( something ); } let getSomething = async function(){ let a = await getPromise(); return a; } The async function returns a promise object, so there is no way to return a non-promise ob...
can be run in Chrome. The program details are shown below: will the current task queue be rolled back to the status of the Project sibling as soon as you execute reject,? ...
perPageFiles = filenames.slice(articleIndex, articleIndex + perPage); perPageFiles.forEach(function(filename, index) { fs.readFile(fileDirectory + filename + .md , utf-8 , function(err, data) { if (err) throw err; perPageDatas[index].arti...
Database query function query(sql) { return new promise((resolve, reject) => { pool.getConnection(function (err, conn) { if (err) { reject(err); } else { conn.query(sql, function (e...
as shown in the figure above, what is not clear is: why then uses dot syntax, while catch and finally use [] syntax because catch and finally are keywords in js? I don t know what I don t know about this. this is the github address of this polyfi...
Both then and catch in promise can handle exceptions, so what s the difference between them in handling exceptions? Promise.reject( error ).then(()=> {}, val=> { console.log(val); }); Promise.reject( error ).catch(val=> { console...
fetch( http: 10.3.134.173 jsonp-test data dish_getbypage.php?start=1 , ).then((response)=> response.json() ).then((res)=>{ console.log(res[0].name); }); fetch( http: 10.3.134.173 jsonp-test data dish_getbypage.php?s...
function loadUploadFn() { return new Promise(function(resolve, reject) { var len = 0; ** * var uploadListIns = $upload.render({ elem: -sharpuploadBtn , url: .. accountManager uploadWechatCod...
the return of err and catch does not default to reject. new Promise((resolve, reject) => { setTimeout(() => { if (0) { resolve(10) } else { reject(11) } }, 0) ...
how to reset resolve or Promise after the first resolve so that the next resolve has output I am writing login operations, and sometimes I encounter login failures. You need to reset resolve or Promise, so that you can have resolve when you initiate a...
when using bluebird and nodejs native Promise to execute the following code, the order of execution is somewhat different. What is the reason for this? ** * bluebird, 2 3 5 6 1 4 * Promise 2 3 5 6 4 1 * var Promise = require("bluebird"...
function loadImageAsync(url) { return new Promise(function(resolve, reject) { throw new Error( BUG FOREVER ) const image = new Image(); image.onload = function() { resolve(image); }; image.o...
the second interface uses the data returned by the first interface name , and then splices it without promise.all or global variables. Solve? function ajax(url,datas){ return new Promise((resolve,reject) => { $.ajax({...
The code is as follows: how can I capture the reject? of any previous task in the tail catch var task1 = new Promise(function(resolve, reject){ resolve( success ); }); var task2 = new Promise(function(resolve, reject){ reject( error ); }); t...
< H1 > Code < H1 > undefined (node:5784) UnhandledPromiseRejectionWarning: TypeError: Cannot read property name of undefined at say (D: NodeJS Test test2.js:18:22) ...