yukaii / synchronized-promise Goto Github PK
View Code? Open in Web Editor NEWTurn ES6 Promise into synchronize function call, a simple wrapper of deasync package
Home Page: https://www.npmjs.com/package/synchronized-promise
License: MIT License
Turn ES6 Promise into synchronize function call, a simple wrapper of deasync package
Home Page: https://www.npmjs.com/package/synchronized-promise
License: MIT License
This lib will not work when called from an async context due to an issue with deasync
https://github.com/abbr/deasync/issues/21
https://gist.github.com/Yukaii/2f2ba5a3343740af26d65ae5ea2ca412
var data = syncPromise(asyncFunction, true, 10000)()
console.log(data)
// throw timeout error
try {
syncPromise(asyncFunction, false, 1000)()
} catch (e) {
console.log(e)
}
// Promise rejection
try {
syncPromise(asyncFunction, false, 3000)()
} catch (e) {
console.log(e)
}
console.log('aweeeeesome')
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.
如果一个异步方法里面调用了 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
deasync
needs to be bumped to latest fix to support Node 12
See: abbr/deasync#114
https://github.com/jakwuh/deasync-promise
呃,寫完才發現重造輪子,算了不管了 😂
時間控制算是 optional 的
update README
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.