Giter VIP home page Giter VIP logo

synchronized-promise's Introduction

synchronized-promise

All Contributors

npm version Build Status

Turn ES6 Promise into synchronize function call, a simple wrapper of deasync package

Installation

npm install synchronized-promise --save

Usage

First, you need to require the synchronized-promise package, for JavaScript:

const sp = require('synchronized-promise')

For TypeScript or ES6 later JavaScript:

import sp from 'synchronized-promise'

Then call it as the following example:

// An promise base async function
let asyncFunction = (value) => {
  return new Promise((resolve, reject) => {
    setTimeout(function () {
      resolve(value)
    }, 2000)
  })
}

// regular usage
asyncFunction(5).then(value => value === 5)

// make it synchronized
let syncFunc = sp(asyncFunction)
const value = syncFunc(5) // value === 5

See test.js for usage in details.

Contributors ✨

Thanks goes to these wonderful people (emoji key):


侠小然

💻

Clar Charron

💻

Pedro Augusto de Paula Barbosa

📖

This project follows the all-contributors specification. Contributions of any kind welcome!

synchronized-promise's People

Contributors

allcontributors[bot] avatar dependabot[bot] avatar masquevil avatar papb avatar yukaii avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

synchronized-promise's Issues

似乎不支持 this?

如果一个异步方法里面调用了 this,似乎是不支持的,有办法支持吗?

例子:

class A {
  constructor() { this.x = 1; }
  async getX() { return this.x; }
}
const a = new A();
sp(a.getX)();
// 会报错  Cannot read property 'x' of undefined

It's very slow for me.

This package is cool it makes something possible (in workaround situations) that should not be.. but from my initial tests it's very slow.

Following test case: (node v18.14.0)

const sp = require('synchronized-promise');
const a = async function( x ) { return x + 1; }
const b = function b( x ) { return x + 1; }
const n = 50;

console.time( 'simple sync');
let v = 0;
for( let i = 0; i < n; i++ )
{
	v = b( v );
}
console.timeEnd( 'simple sync');

console.time( 'synchornized promise');
const as = sp( a );
v = 0;
for( let i = 0; i < n; i++ )
{
	v = as( v );
}
console.timeEnd( 'synchornized promise');


console.time( 'classic async');
( async function test( )
{
	v = 0;
	for( let i = 0; i < n; i++ )
	{
		v = await a( v );
	}
	console.timeEnd( 'classic async');
}) ( );

classic async has to be last test, since the main loop would continue straight away.

Result:

$ node src/a.js 
simple sync: 0.044ms
synchornized promise: 5.022s
classic async: 0.136ms

As expected promises are a tad slower than direct calls.. but syncronized promises, is way out there in being extremly slow (the code generator I wanted to use it in, where I have an await edge case turns from being almost instant to .. wait a minute or two).

I'm okay with again some overhead to await/async, but this is a bit too much.

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.