Skip to content

Instantly share code, notes, and snippets.

@chekit
Last active May 23, 2016 17:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chekit/47a4786e11efedfeb6eb477374f43c73 to your computer and use it in GitHub Desktop.
Save chekit/47a4786e11efedfeb6eb477374f43c73 to your computer and use it in GitHub Desktop.
Gets an array values consistently, read them and resolve with results (this is a basic example that was done to understand how I can read array, make an ajax call with value and resolve for getting next one). The values are printing in arow.
'use strict';
//The sandbox http://jsbin.com/leraku/edit?js,console
let mas = [1, 2, 3];
let prom = function (value) {
return new Promise(resolve => {
setTimeout(() => {
console.log(value);
resolve();
}, 400);
});
}
let seq = function (arr) {
let ind = 0;
function next () {
if (ind < arr.length) {
prom(arr[ind++])
.then(next);
}
}
next();
}
seq(mas);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment