Research
Security News
Quasar RAT Disguised as an npm Package for Detecting Vulnerabilities in Ethereum Smart Contracts
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Use cb-q to handle callbacks as a deferred object when a callback exists or create a deferred object when a callback does not exist.
npm install cb-q
Promises is a great pattern but many libraries use Error-First Callbacks. Since the promise pattern returns a deferred object and asyncronous functions require a callback and usually return undefined, why can't we do both.
Create an async function than handel both callbacks and promises.
var cbQ = require('cb-q');
asyncFunction(foo, cb()){
var deferred = cbQ.defer(cb);
//do some async stuff
process.nextTick(function(){
//on success
if(success){
deferred.resolve("yes it worked")
} else {
// on error
deferred.reject();
}
})
return deferred.promise;
}
Creating a callback that I can pass into a method expecting an error-first callback and expose a promise.
var cbQ = require('cb-q');
var callback = cbQ.cb();
callback.promise.then(function(result){
//handle success
}, function(err){
//handle error
});
fs.readFile('some-file.txt', callback);
If you already have a promise and simply want to execute a callback when the promise is resolve
var cbQ = require('cb-q');
var deferred = q.defer();
var promise = deferred.promise;
deferred.resolve('resolved');
var callback = function(err, result){
if(err){
throw(err);
}
console.log(result);
}
cbQ.resolve(promise, callback);
fs.readFile('some-file.txt', callback);
npm test
FAQs
Converts callbacks to promises and creates promises from callbacks.
We found that cb-q demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Security News
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.