2

Sorry in advance if somebody is wounded by my silly question, but anyway:

I always thought that await can work properly only with native Promises. But today I encountered that bluebirdjs promises are also properly handled by await.

My questions are:

1) How and why does await really work with non-native Promises?

2) How can I implement my own Promise object which will work correctly with await?

const BluebirdPromise = require('bluebird');

function sleep(mls) {
  return new BluebirdPromise(resolve => setTimeout(resolve, mls));
}

async function test() {
  await sleep(5000); // waits 5000 mls
  console.log('done'); // printed after 5000 mls
}

test();
Roman Roman
  • 807
  • 9
  • 26
  • 2
    I hope the duplicate question (and the posts linked from there) should answer all your questions. If not, please comment. – Bergi Jan 20 '19 at 16:14
  • tl;dr *(I may be wrong here but this is what I understant)* ES6 can async-await any Promise. (A function that has resolve-reject callback.) – darth-coder Jan 21 '19 at 09:52

0 Answers0