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

如何将 node 标准的回调 API 在 fibjs 内转为"同步"调用形式(一面) #9

Open
tapir-dream opened this issue Apr 30, 2019 · 1 comment
Assignees

Comments

@tapir-dream
Copy link

tapir-dream commented Apr 30, 2019

To:
Tapir

面试公司:
未知

面试环节:
一面

问题:
如何将 node 标准的回调 API 在 fibjs 内转为"同步"调用形式

@tapir-dream
Copy link
Author

fibjs 可以使用 util.sync 将 callback 或者 async 函数转变为 sync 函数,并且直接调用。

callback 例子:

var util = require('util');
function cb_test(a, b, cb) {
    setTimeout(() => {
        cb(null, a + b);
    }, 100);
}

var fn_sync = util.sync(cb_test);
console.log(fn_sync(100, 200));

async 例子:

var util = require('util');
async function async_test(a, b) {
    return a + b;
}
var fn_sync = util.sync(async_test);
console.log(fn_sync(100, 200));

async/await 例子:

var util = require('util');
function async_test(a, b) {
    return new Promise(function(resolve, reject) {
        resolve(a + b);
    });
}
var fn_sync = util.sync(async_test, true);
console.log(fn_sync(100, 200));

@acodercc acodercc changed the title To Tapir: 如何将 node 标准的回调 API 在 fibjs 内转为"同步"调用形式 如何将 node 标准的回调 API 在 fibjs 内转为"同步"调用形式(一面) May 6, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants