Skip to content

Instantly share code, notes, and snippets.

@limweb
Forked from anvk/promises_reduce.js
Created November 2, 2017 04:56
Show Gist options
  • Save limweb/4b06bc553b617b8cb5cd9d2c3eb2a4c9 to your computer and use it in GitHub Desktop.
Save limweb/4b06bc553b617b8cb5cd9d2c3eb2a4c9 to your computer and use it in GitHub Desktop.
Sequential execution of Promises using reduce()
function asyncFunc(e) {
return new Promise((resolve, reject) => {
setTimeout(() => resolve(e), e * 1000);
});
}
const arr = [1, 2, 3];
let final = [];
function workMyCollection(arr) {
return arr.reduce((promise, item) => {
return promise
.then((result) => {
console.log(`item ${item}`);
return asyncFunc(item).then(result => final.push(result));
})
.catch(console.error);
}, Promise.resolve());
}
workMyCollection(arr)
.then(() => console.log(`FINAL RESULT is ${final}`));
@limweb
Copy link
Author

limweb commented Nov 2, 2017

			for (let chunkobj of chunks) {
				console.log('bf',chunkobj)
				let rs = 	await axios.post("/ajax/shippopbook",{chunkobj})
				console.log('af',rs)
				this.$nty.confirm('job #'+chunkobj.chunk+'/'+chunks.length+' successed');
			}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment