DEV Community

Vahe
Vahe

Posted on

4 2

Turn any callback into promise with babel-plugin-promise

briefly,I will tell you how to convert an existing callback to promise.

We will need to use babel.

For example, let's take a request from npm and convert it to a promise

yarn add babel-plugin-promise --dev
Enter fullscreen mode Exit fullscreen mode
require("babel-polyfill");

import request from "request";

//@promisify<err,res> req
__ = request(arg1);

console.log(req("http://www.google.com"));

req("http://www.google.com")
    .then(res => console.log(res.body.length))
    .catch(err => console.log(err));

Enter fullscreen mode Exit fullscreen mode

let's check

quite quickly

babel-plugin-promise

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

Top comments (0)

SurveyJS custom survey software

JavaScript Form Builder UI Component

Generate dynamic JSON-driven forms directly in your JavaScript app (Angular, React, Vue.js, jQuery) with a fully customizable drag-and-drop form builder. Easily integrate with any backend system and retain full ownership over your data, with no user or form submission limits.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay