Comments (1)
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));
from fe9-interview.
Related Issues (20)
- 微店: 将二维数组转成一维数组的方法有哪些(一面) HOT 1
- 有赞: 为什么在类实例中可以访问this.setState (二面) HOT 1
- 有赞: 介绍暂时性死区(一面) HOT 2
- 兑吧: prototype 和 proto 区别(一面) HOT 1
- 今日头条: 介绍下Promise,内部实现(一面) HOT 1
- 有赞: 在不改变原数组的前提下,添加或删除某个元素(一面) HOT 2
- 阿里巴巴: 使用伪码实现 npm install 部分内容(二面)
- 有赞: sum(2, 3)实现sum(2)(3)的效果(一面) HOT 1
- To Tom0121 前端开发中设计到什么模式 HOT 1
- 滴滴: JS异步解决方案的发展历程以及优缺点(一面) HOT 1
- 携程:数组去重和数组扁平化(一面) HOT 1
- 阿里巴巴:实现一些主流框架的循环渲染(笔试) HOT 1
- To littleKnave: 实现一个Promise.prototype.first(今日头条) HOT 1
- 阿里巴巴:浏览器的强缓存和协商缓存(一面) HOT 3
- 51 信用卡: apply,call,bind 的区别(一面) HOT 2
- 为什么使用 ctrl+f5 或启用开发者面板中 Disable cache 选项后还会存在读取缓存的现象 HOT 1
- 阿里:Koa-body原理(一面)
- 爱云校:算法两道(一、1-100所有的素数 二、快速排序)(笔试) HOT 2
- 腾讯:js中精度问题及解决方案(一面) HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from fe9-interview.