Code Monkey home page Code Monkey logo

async-await-codemod's Introduction

async-await-codemod

Build Status Code Style: prettier

This repository contains a codemod script for use with JSCodeshift that migrates promise-based functions to use async/await syntax.

The excellent sinon-codemod repository was used as inspiration and served as a template for this repository.

This codemod is based in part on work done by @cpojer https://github.com/cpojer/js-codemod/pull/49/commits/19ed546d8a47127d3d115f933d924106c98e1b8b and the further work of @cassilup https://github.com/cassilup/async-await-codemod-demo

Setup & Run

  • npm install -g jscodeshift
  • git clone https://github.com/sgilroy/async-await-codemod.git or download a zip file from https://github.com/sgilroy/async-await-codemod/archive/master.zip
  • Run npm install in the async-await-codemod directory
    • Alternatively, run yarn to install in the async-await-codemod directory
  • jscodeshift -t <codemod-script> <path>
  • Use the -d option for a dry-run and use -p to print the output for comparison

async-await

ES2017 natively supports a special syntax for working with promises called "async/await".

With promises:

function makeRequest() {
  return getJSON().then(data => {
    console.log(data);
    return 'done';
  });
}

With async/await:

async function makeRequestFunction() {
  const data = await getJSON();
  console.log(data);
  return 'done';
}

Included Scripts

async-await

Converts each asynchronous function (a function which contains a .then() call) to async, and uses await instead of .then() to simplify the behavior of using promises synchronously.

jscodeshift -t async-await-codemod/async-await.js <path>

await-promise-chain

Unravels chained promise calls of the style foo.then().then() as multiple await calls. Note that this changes the structure and scope of blocks of code and can thus result in different behavior, such as by variables being in scope that otherwise would not.

This should generally be used after the async-await codemod, and the changes should be examined and tested carefully to avoid unwanted bugs or subtle problems.

jscodeshift -t async-await-codemod/await-promise-chain.js <path>

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.