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}`); });