Andrey (azangru) wrote,
Andrey
azangru

Маленький эксперимент: как превратить массив неких значений в цепочку последовательно выполняемых асинхронных функций в js-е. С новым (еще не вошедшим в стандарт) async-await-ом — очень удобно:

const foo = async function (arr) {
  for (let i = 0; i < arr.length; i++) {
    let x = await bar(arr[i]);
    console.log(x);
  }
}

const bar = function (x) {
  console.log('x is', x);
  return new Promise((resolve) => {
    setTimeout(() => resolve(x), 1000);
  })
};

foo([1, 2, 3])


(пример выполнения в бабелевском репле)

(Это мне понадобилось, эксперимента ради, превратить массив урлов в цепочку последовательно выполняемых запросов)
Subscribe

  • (no subject)

    Someone who self-identifies as a technical writer, among other things, begins her article How To Build An E-Commerce Site With Angular 11, Commerce…

  • (no subject)

    Today I learned that "open-source" does not mean just having the source code in the open. I thought the legalities were only around the term "free…

  • (no subject)

    Cute; but as far as I understand, it is React that innovates on the apis and Preact (and others) that follows to maintain compatibility or…

  • Post a new comment

    Error

    default userpic
    When you submit the form an invisible reCAPTCHA check will be performed.
    You must follow the Privacy Policy and Google Terms of use.
  • 0 comments