JSPM

promisefy-util

1.2.0
    • ESM via JSPM
    • ES Module Entrypoint
    • Export Map
    • Keywords
    • License
    • Repository URL
    • TypeScript Types
    • README
    • Created
    • Published
    • Downloads 3
    • Score
      100M100P100Q53298F
    • License MIT

    promisefy util

    Package Exports

    • promisefy-util

    This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (promisefy-util) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

    Readme

    promisefy-util

    Convert function with callback to promise

    common function with callback

    function func(para, _cb) {
        setTimeout(()=>{
            let err = null;
            let result = para*2;
            _cb(err, result);		
        }, para);
    }

    convert to promise

    let ret;
    try {
        ret = await pf.promisefy(func, [100]);
    }catch(err){
        console.log("Err: ", err);
        return;
    }

    array forEach with promise

    let v = [2000, 3000, 1000];
    let ps = v.map(function(item, index){
        return pf.promisefy(func, [item]);
    })	
    let ret = await Promise.all(ps);
    console.log("ret: ", ret);