We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
手动实现一个promisify函数的意思是:我们把一个异步请求的函数,封装成一个可以具有 then方法的函数,并且在then方法中返回异步方法执行结果的这么一个函数
// 首先定一个需要进行 promisify 的函数 function asyncFn(a, b, callback) { // 异步操作,使用 setTimeout 模拟 console.log('异步请求参数', a, b) setTimeout(function() { callback('异步请求结果') }, 3000) } // 我们希望调用的方式是 const proxy = promisify(asyncFn) proxy(11,22).then(res => { // 此处输出异步函数执行结果 console.log(res) }) // 定义一个方法, 需要针对异步方法做封装,所以需要一个入参,既需要promisify的原异步方法 function promisify(func) { return function(...args) { return new Promise((resolve, reject) => { const callback = (err, data) => { if (err) { reject(err); } else { resolve(data) } }; func.call(this, ...args, callback); }); } }
The text was updated successfully, but these errors were encountered:
function promisify(fn) { return function(...args) { return new Promise((resolve, reject) => { fn(...args, (err, data) => { if (err) { reject(err); } else { resolve(data); } }); }); }; }
Sorry, something went wrong.
No branches or pull requests
手动实现一个promisify函数的意思是:我们把一个异步请求的函数,封装成一个可以具有 then方法的函数,并且在then方法中返回异步方法执行结果的这么一个函数
The text was updated successfully, but these errors were encountered: