Last active
December 22, 2019 09:30
-
-
Save BananaAcid/2386e7d19f0db15985050b86c2a198ec to your computer and use it in GitHub Desktop.
Promise/Callback fn wrappers for then-pug (which is generators based)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//- will only work with the `npm install then-pug` module instead of default `pug` | |
//- passed in locals are: sendmail (node module), insp which is require('util').inspect, and just passed in Promise | |
- | |
// for functions having the second param as callback (e.g. nodejs fs.readFile) | |
function syncifyCb(fn) { | |
return function* (param1) { | |
let result = null; | |
yield function* () { | |
yield new Promise(function (ret) { | |
fn( | |
param1 || undefined, | |
function(err,data) { ret({err,data}); } | |
); | |
}) | |
}().next().value.then(function(r) { result = r; }); | |
return result; | |
} | |
}; | |
//syncify | |
let sendmailSync = syncifyCb(sendmail); | |
//exec | |
let res = yield sendmailSync({ | |
from: 'no-reply@test.test', | |
to: 'test@test.test', | |
subject: 'test sendmail', | |
html: 'Hello world.' | |
}); | |
pre= insp(res) | |
- | |
// for directly wrapping a promise to await for | |
function syncifyPromise(fn) { | |
return function* () { | |
let result = null; | |
yield function* () { | |
yield fn | |
}().next().value.then(function(r) { result = r; }); | |
return result; | |
}() | |
}; | |
const start = Date.now(); // mesure the 1s | |
// new Promise stars resolving async the moment it is declared - syncify + exec | |
let res2 = yield syncifyPromise( | |
// test promise | |
new Promise( function(ret) { | |
setTimeout(function() {ret('sleep'); }, 1000); // 1s delay | |
}) | |
); | |
const ms = Date.now() - start; | |
pre= insp(res2) + ` -> ${ms}ms` | |
- | |
// for typical facory functions returning a promise to be awaited for (e.g. node fs.promises.readFile) | |
function syncifyPromiseFactory(fn) { | |
return function* (param1) { | |
let result = null; | |
yield function* () { | |
yield fn(param1) | |
}().next().value.then(function(r) { result = r; }); | |
return result; | |
} | |
}; | |
// test factory | |
let makeX = function(options = {}) { | |
return new Promise( function(ret) { | |
setTimeout(function() {ret('sleep'); }, options.ms || 1000); // 1s delay | |
}); | |
}; | |
// syncify | |
let makeXSync = syncifyPromiseFactory(makeX); | |
const startX = Date.now(); // mesure the 1s | |
// exec | |
let res3 = yield makeX({ms: 500}); | |
const msX = Date.now() - startX; | |
// example2 - passed in on locals: {.., CouchDB: nano(serviceUrlInclCreds), .. } | |
//let list = syncifyPromiseFactory( CouchDB.db.use('requests').list ); | |
//let requests = yield list(); | |
pre= insp(res3) + ` -> ${msX}ms` | |
div done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment