Speak now
Please Wait Image Converting Into Text...
Embark on a journey of knowledge! Take the quiz and earn valuable credits.
Challenge yourself and boost your learning! Start the quiz now to earn credits.
Unlock your potential! Begin the quiz, answer questions, and accumulate credits along the way.
General Tech Bugs & Fixes 2 months ago
Posted on 27 Aug 2024, this text provides information on Bugs & Fixes related to General Tech. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.
Turn Your Knowledge into Earnings.
I want to work with promises but I have a callback API in a format like:
window.onload; // set to callback ... window.onload = function() { };
function request(onChangeHandler) { ... } request(function() { // change happened ... });
function getStuff(dat, callback) { ... } getStuff("dataParam", function(err, data) { ... })
API; API.one(function(err, data) { API.two(function(err, data2) { API.three(function(err, data3) { ... }); }); });
You can do something like this
// @flow const toPromise = (f: (any) => void) => { return new Promise<any>((resolve, reject) => { try { f((result) => { resolve(result) }) } catch (e) { reject(e) } }) } export default toPromise
Then use it
async loadData() { const friends = await toPromise(FriendsManager.loadFriends) console.log(friends) }
es6-promisify converts callback-based functions to Promise-based functions.
es6-promisify
const promisify = require('es6-promisify'); const promisedFn = promisify(callbackedFn, args);
Ref: https://www.npmjs.com/package/es6-promisify
My promisify version of a callback function is the P function:
callback
P
var P = function() { var self = this; var method = arguments[0]; var params = Array.prototype.slice.call(arguments, 1); return new Promise((resolve, reject) => { if (method && typeof(method) == 'function') { params.push(function(err, state) { if (!err) return resolve(state) else return reject(err); }); method.apply(self, params); } else return reject(new Error('not a function')); }); } var callback = function(par, callback) { var rnd = Math.floor(Math.random() * 2) + 1; return rnd > 1 ? callback(null, par) : callback(new Error("trap")); } callback("callback", (err, state) => err ? console.error(err) : console.log(state)) callback("callback", (err, state) => err ? console.error(err) : console.log(state)) callback("callback", (err, state) => err ? console.error(err) : console.log(state)) callback("callback" REPLY 1 views 0 likes 1 shares Facebook Twitter Linked In WhatsApp
No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.
General Tech 9 Answers
General Tech 7 Answers
General Tech 3 Answers
General Tech 2 Answers
Ready to take your education and career to the next level? Register today and join our growing community of learners and professionals.