Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

将minapp-generator的tsconfig.json的target调整为es2017会提示done函数找不到 #43

Open
AlexStacker opened this issue Mar 31, 2018 · 3 comments

Comments

@AlexStacker
Copy link

本来想调试简单一些,直接使用node的async、await,就将target调整为es2017,不用typescript的tslib,结果报done不存在了。
如果调整target为es2017的话async库的调用方式不一样,会走asyncify不传递callback

// async.js L192-211
function asyncify(func) {
    return initialParams(function (args, callback) {
        var result;
        try {
            result = func.apply(this, args);
        } catch (e) {
            return callback(e);
        }
        // if result is Promise object
        if (isObject(result) && typeof result.then === 'function') {
            result.then(function(value) {
                invokeCallback(callback, null, value);
            }, function(err) {
                invokeCallback(callback, err.message ? err : new Error(err));
            });
        } else {
            callback(null, result);
        }
    });
}

我将代码调整了一下,大佬要不要参考一下

// cmd.ts L86
let result = await new Generator(key, node, nodeUrl, nodeSource, genDir)
  .exec(res.markdown, res.promise)
  .then((str) => {
    if (!allResult) allResult = []
    if (str) allResult.push(str)
    return allResult;
  })
  //.catch((e: any) => done(e))

  if(done) {
    done(undefined, result)
  } else {
    return result;
  }
@qiu8310
Copy link
Owner

qiu8310 commented Apr 1, 2018

我不能复现你的问题, done 都是存在的。我觉得也不应该,async 文档上是说那样用的,一般不可能不同的 node 版本出现两种不同的调用方式吧

@AlexStacker
Copy link
Author

确定没出现吗?我用的node8,tsconfig.json的target修改成es2017,vscode debug然后调用确实变了,修改前后两次不一样,执行的async库没有将done传过去,而是先执行,根据结果判断再调用done

@AlexStacker
Copy link
Author

// async.js L2498-L2509
function reduce(coll, memo, iteratee, callback) {
    callback = once(callback || noop);
    var _iteratee = wrapAsync(iteratee);
    eachOfSeries(coll, function(x, i, callback) {
        _iteratee(memo, x, function(err, v) {
            memo = v;
            callback(err);
        });
    }, function(err) {
        callback(err, memo);
    });
}
// async.js L225-233
var supportsSymbol = typeof Symbol === 'function';

function isAsync(fn) {
    return supportsSymbol && fn[Symbol.toStringTag] === 'AsyncFunction';
}

function wrapAsync(asyncFn) {
    return isAsync(asyncFn) ? asyncify(asyncFn) : asyncFn;
}

又debug了一下流程,生成出来的js reduce后会走wrapAsync封装一次,然后走了asyncify才会出现的done找不到。用的是node8.9.3、typscript2.8.1应该和这个没关系吧。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants