promisify a function has More than two params
HoKangInfo opened this issue ยท 5 comments
v1.11
promisify a function that callback function has More than two params
e.q.
const { exec } = require('child_process');
const exec_1 = require('util').promisify(exec);
const exec_2 = require('aigle').promisify(exec);
const cmd = 'ls 1>&2';
exec_1(cmd).then(raw=> console.log('exec 1=>',raw)).catch(e=>console.log('exec 1 error=>',e));
exec_2(cmd).then(raw=> console.log('exec 2=>',raw)).catch(e=>console.log('exec 2 error=>',e));
.result
exec 1 => { stdout: '', stderr: 'a.file'}
exec 2 =>
Currently util.promisify
handles some non-standard callbacks via the internal symbol as here.
For your case, exec
's custom promise was defined under here.
So we can check if there is any custom promise like this.
const { exec } = require('child_process');
const { promisify } = require('util');
if (exec[promisify.custom]) {
// fallback to use the custom promise
}
There is other custom promises too.
Examples:
- child_process.exec
- child_process.execFile
- dns.lookup
- dns.lookupService
- fs.read
- fs.write
Btw, we need to implement this in Node.js environment only and just skip the checks in browser.
Thank for your reply.
I understand child_process.exec is non-standard callback.
Just to discuss need or not support.
@HoKangInfo @thammin Thanks for the issue, it is so helpful!
Yes, let's support it! ๐
we need to implement this in Node.js environment only and just skip the checks in browser.
Sure, we need to handle it. ๐
I published it on v1.12.0-alpha.7
.
But I just realized the promisified function doesn't return Aigle instances. I'll think about it more.
I published it on v1.12.0
!