Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>
</body>
</html>
 
const urls = ['url1', 'url2', 'url3'];
function fakeFetch(url, params = '-') {
  // этот вывод в консоль покажет порядок вызовов с их входящими параметрами
  console.log(`fakeFetch to: ${url} with params: ${params}`);
  return new Promise(resolve => {
    setTimeout(() => resolve(`${url} is DONE`), 1000);
  })
};
function generatorWay(callback) {
  function* generateSequence() {
    let results;
    // noprotect
    for (let url of urls) {
      results = yield fakeFetch(url, results);
    }
    return results;
  }
  function execute(generator, yieldValue) {
    let next = generator.next(yieldValue);
    if (!next.done) {
      return next.value
        .then(result => execute(generator, result));
    }
    callback(next.value);
  }
  execute(generateSequence())
}
generatorWay(result => console.log(`result: ${result}`))
Output

You can jump to the latest bin by adding /latest to your URL

Dismiss x
public
Bin info
andr213pro
0viewers