Asynchronous Functions to Promises

JavaScriptJavaScriptBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

In this lab, we will learn how to convert an asynchronous function to return a promise using the promisify function. This will allow us to write cleaner and more readable code by avoiding callback functions. We will also explore the use of currying and the rest operator in JavaScript.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL javascript(("`JavaScript`")) -.-> javascript/BasicConceptsGroup(["`Basic Concepts`"]) javascript(("`JavaScript`")) -.-> javascript/AdvancedConceptsGroup(["`Advanced Concepts`"]) javascript(("`JavaScript`")) -.-> javascript/ToolsandEnvironmentGroup(["`Tools and Environment`"]) javascript/BasicConceptsGroup -.-> javascript/variables("`Variables`") javascript/BasicConceptsGroup -.-> javascript/data_types("`Data Types`") javascript/BasicConceptsGroup -.-> javascript/arith_ops("`Arithmetic Operators`") javascript/BasicConceptsGroup -.-> javascript/comp_ops("`Comparison Operators`") javascript/AdvancedConceptsGroup -.-> javascript/async_prog("`Asynchronous Programming`") javascript/AdvancedConceptsGroup -.-> javascript/spread_rest("`Spread and Rest Operators`") javascript/ToolsandEnvironmentGroup -.-> javascript/debugging("`Debugging`") subgraph Lab Skills javascript/variables -.-> lab-28559{{"`Asynchronous Functions to Promises`"}} javascript/data_types -.-> lab-28559{{"`Asynchronous Functions to Promises`"}} javascript/arith_ops -.-> lab-28559{{"`Asynchronous Functions to Promises`"}} javascript/comp_ops -.-> lab-28559{{"`Asynchronous Functions to Promises`"}} javascript/async_prog -.-> lab-28559{{"`Asynchronous Functions to Promises`"}} javascript/spread_rest -.-> lab-28559{{"`Asynchronous Functions to Promises`"}} javascript/debugging -.-> lab-28559{{"`Asynchronous Functions to Promises`"}} end

Promisify Function

To convert an asynchronous function to return a promise, follow these steps:

  1. Open the Terminal/SSH and type node to start practicing coding.
  2. Use currying to return a function that returns a Promise which calls the original function.
  3. Use the rest operator (...) to pass in all the parameters.
  4. If you are using Node 8+, you can use util.promisify.
  5. Here's an example code snippet:
const promisify =
  (func) =>
  (...args) =>
    new Promise((resolve, reject) =>
      func(...args, (err, result) => (err ? reject(err) : resolve(result)))
    );
  1. To use this function, define the asynchronous function and pass it as a parameter to the promisify function. The returned function will now return a promise.
const delay = promisify((d, cb) => setTimeout(cb, d));
delay(2000).then(() => console.log("Hi!")); // Promise resolves after 2s

The delay function is an example of an asynchronous function that now returns a promise using the promisify function.

Summary

Congratulations! You have completed the Promisify Function lab. You can practice more labs in LabEx to improve your skills.

Other JavaScript Tutorials you may like