class Parse{ get(){ let arr = { 'first' : 'Not change', 'second' : 'Not change' } // Функция 1 setTimeout(()=>{ console.log('Firstй') arr.pop = 'change' }, 5000) // Функция 2 setTimeout(()=>{ console.log('Second') arr.sdd = 'change' }, 3000) return arr } } let pars = new Parse() arr = pars.get() console.log(arr) 

Need to perform two asynchronous functions in succession and return the result?

Reported as a duplicate at Grundy. javascript Oct 16 '17 at 7:02 .

A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .

    1 answer 1

    Closest to issue code

    To get the result asynchronously, you can return from the Promise function and take the result from it:

     pars.get().then(r=>console.log(r)); 

    here pars.get returns a Promise, and the then method defines the function that will be called when the promise resolves. In this case, the argument of this function r will contain the argument with which resolve was called, that is, the arr array:

     resolve(arr); 

    In the get function, you must return a new promise. When creating a promise, a function is passed to it, which will be executed asynchronously. Upon completion, you must call resolve and pass the result to this call:

     return new Promise((resolve) =>{ setTimeout(()=>{ console.log('First') arr.pop = 'change' setTimeout(()=>{ console.log('Second') arr.sdd = 'change' resolve(arr); }, 3000); }, 5000); }); 

    The working version may look like this:

      class Parse{ get(){ let arr = { 'first' : 'Not change', 'second' : 'Not change' } return new Promise((resolve) =>{ setTimeout(()=>{ console.log('First') arr.pop = 'change' setTimeout(()=>{ console.log('Second') arr.sdd = 'change' resolve(arr); }, 3000); }, 5000); }); } } let pars = new Parse() pars.get().then(r=>console.log(r)); 

    General sequence call

    Several asynchronous functions are created. Each asynchronous function must return a Promise. Promise should rezolvitsya result of the asynchronous function. For example, the following function returns a promis, “inside” of which the html page is retrieved and the promise resolves with an argument that represents the content of the page:

     function getHtml() { return new Promise((resolve, reject) => { const html = "html content"; resolve(html); }); } 

    Promise has a method then. This method takes at least one parameter — a function that will be called when resolve is called in promise. This function parameter takes the value with which resolve was called. In this function, you can process the result of the asynchronous function, and if necessary, start a new asynchronous function and return its result (ie, a new Promise):

     getHtml() .then(r => { // делаем что-то с содержимым html return getImage(r); }) 

    then you can connect then calls in a chain, sequentially processing the results of asynchronous functions. If it is required, for example, to get an html page, on the basis of html to get an image containing text and recognize the text, then the whole chain may look like this:

     function getHtml() { return new Promise((resolve, reject) => { const html = "html content"; resolve(html); }); } function getImage() { return new Promise((resolve, reject) => { const image = "image"; resolve(image); }); } function recognize() { return new Promise((resolve, reject) => { const text = "text"; resolve(text); }); } getHtml() .then(r => { // делаем что-то с содержимым html return getImage(r); }) .then(r => { // делаем что-то с изображением return recognize(r); }) .then(r => { // просто выводим результат console.log(`result: ${r}`); }) .catch(e => { // что-то пошло не так console.log(`error: ${e}`); }); 

    • Some dirty code in your response. Several asynchronous functions together, the variable arr , and in it an object, where const is needed, let be used. - ReklatsMasters
    • @ReklatsMasters I tried to write as close as possible to the author's code so that the coherent differences were visible. A few asynchronous functions together is a pretty good way to consistently perform two setTimeout - tilin
    • Under what conditions can resolve fail? I am writing a parser. The sequence of actions is as follows: 1. Get the Html page with parsim all that is possible from it. 2. Run the browser emulator (Nightmare.js) with the help of it we get a picture with add. information. 3. We recognize the text from the image using (tesseract). Resolve does not want to be called when performing 3 points. Are there any restrictions in the call? Those. I got 3 nested asynchronous f-ii. - Artem Zvaygzne
    • @Artem Zvaigzne updated the answer - tilin