Async/await can still surprise you... A LOT!

Did you know you can use async/await with any "thenable" function, not only with Promises()?!

I love technology ❤, and the fact that it doesn't matter how much we know there will always be something to amaze us. Today a friend of mine (@Rafael_Toscano) showed me something that my instant reaction was like this:

Cat Surprised

He shared with me an article from the V8 blog about "Faster async functions and promises." and among all sort of exciting things, one captured my attention in a way that I could only think "This can't be real, I have to test it."

It regards the async/await behavior and the fact you can use async/await with any "thenable" function. What does it mean? Any object which has a ".then" method can be used with async/await.

In the article, he gives the following example:

class Sleep {
  constructor(timeout) {
    this.timeout = timeout;
  }
  then(resolve, reject) {
    const startTime = Date.now();
    setTimeout(() => resolve(Date.now() - startTime),
               this.timeout);
  }
}

(async () => {
  const actualTime = await new Sleep(1000);
  console.log(actualTime);
})();

Mother Of God

Yes, please tell me I am not the only one whose mind was blown by seeing that.

I think this helps us understand a little bit more about async/await functions and the possibilities of things we can do in our code. But also it comes with great responsibility, please don't replace simple promises everywhere by this only because it feels nice.

Only use it if you can find exceptional use cases, and if you do, please share it with us in the comments. I do love to hear about it! I'm considering if to implement a "retry strategy" wouldn't be a good opportunity for this use as following the idea in the bellow code.

const ServerMock = {
  count: 0,
  getData() {
    if (this.count === 2) {
      return Promise.resolve([{ name: "SomeName" }]);
    } else {
      this.count++;
      return Promise.reject("Error");
    }
  }
};

function fetchData(limit, time) {
  return {
    then(resolve, reject) {
      const retry = setInterval(async () => {
        limit--;
        try {
          console.log("Trying....");
          const data = await ServerMock.getData();
          if (data) {
            console.log("Resolve");
            clearInterval(retry);
            resolve(data);
          }
        } catch {
          if (limit === 0) {
            clearInterval(retry);
            reject("Limit Reached");
          }
        }
      }, time);
    }
  };
}

(async () => {
  try {
    const result = await fetchData(3, 1000);
    console.log(result);
  } catch (err) {
    console.log(err);
  }
})();

Let me know what you think about it in the comments.

Comentários