bluebird

Trying to understand how promisification works with BlueBird

99封情书 提交于 2019-12-28 12:07:06
问题 I'm trying to wrap my head around promises using the Bluebird library for Node.js. Below is a simple example that doesn't work as I would expect. var Promise = require("bluebird"); var myObj = { add: function(op1, op2) { return op1 + op2; } }; // Sync call to add method -> 7 console.log(myObj.add(3,4)); var myObjAsync = Promise.promisifyAll(myObj); // Async call to promisified add method -> nothing written to console myObjAsync.addAsync(2,3).then(function(data) { console.log(data); return

Get Bluebird Promise from async await functions

狂风中的少年 提交于 2019-12-28 06:01:14
问题 I am looking for a way, with Node v7.6 or above, to get a Bluebird Promise (or any non-native promise) when an async function is called. In the same way I can do: global.Promise = require('Bluebird'); // Or Q/When var getResolvedPromise = () => Promise.resolve('value'); getResolvedPromise .tap(...) // Bluebird method .then(...); See: May I use global.Promise=require("bluebird") I want to be able to do something like: global.Promise = require('Bluebird'); // Or Q/When var

How do I handle errors with promises?

◇◆丶佛笑我妖孽 提交于 2019-12-27 22:15:53
问题 As a node programmer. I'm used to use "nodebacks" for handling errors in my code: myFn(param, function(err, data) { if (err){ //error handling logic } else { // business logic } }); When writing that function, I can do something like: var myFn = function(param, callback){ var calc = doSomeCalculation(param); if(calc === null) { // or some other way to detect error callback(new Error("error with calculation"), null); } ... someAsyncOp(calcN,function(err, finalResult){ if(err) return callback

How do I handle errors with promises?

ぐ巨炮叔叔 提交于 2019-12-27 22:14:51
问题 As a node programmer. I'm used to use "nodebacks" for handling errors in my code: myFn(param, function(err, data) { if (err){ //error handling logic } else { // business logic } }); When writing that function, I can do something like: var myFn = function(param, callback){ var calc = doSomeCalculation(param); if(calc === null) { // or some other way to detect error callback(new Error("error with calculation"), null); } ... someAsyncOp(calcN,function(err, finalResult){ if(err) return callback

Bluebird promise, promise.each only executes once

≡放荡痞女 提交于 2019-12-25 09:28:03
问题 There is a function called musicPromise(). What this function does is It gets all mp4 files and loop through it. then it tries to convert each mp4 to mp3, using fluent-ffmpeg The problem I am facing is It only converts 1 file, no matter how many mp4 files I have. And it seems never reach to proc.on('end', (x) => { Full code here: // search const glob = require('glob'); // wait for const Promise = require('bluebird'); // fs const fs = require('fs'); // mp3 const ffmpeg = require('fluent-ffmpeg

Bluebird promise is undefined?

让人想犯罪 __ 提交于 2019-12-25 08:29:50
问题 I'm using node 4.5+ and bluebird. I have the following code I intend to use with then : var checkdir = function(directory) { return new Promise(function(resolve, reject) { fs.statAsync(directory).then(function() { resolve(true); }).catch(function(err) { if(err.code === 'ENOENT') { fs.mkdirAsync(directory).then(function() { resolve(true); }).catch(function() { reject(new Error('Can not create folder')); }); } else { reject(new Error('Unknown fs stat error: ' + err)); } }); }); }; Async

Promisify cursor execution: MongoDB Native Driver

天大地大妈咪最大 提交于 2019-12-25 06:23:54
问题 I already read up on these, but I'm having a bit of trouble executing it. mongoDB promise gets returned too early Specifically, I'm trying to promisify the collection.find cursor, but I'm not sure I'm doing it right... any help would be appreciated: //mongo.js var promise = require('bluebird'); var mongodb = require('mongodb'); var mongoClient = mongodb.MongoClient; var collection = mongodb.Collection; promise.promisifyAll(mongodb); promise.promisifyAll(collection.prototype); collection

Promisifying a function with Bluebird

孤人 提交于 2019-12-25 05:50:54
问题 I am new to javascript asynchronous concepts and come from a C++ background. Recently, I realize that some of my functions do not work because they do not return promises. For example, this function; var CSVConverter=require("csvtojson").Converter; function get_json(cvs_file_location) { var data=fs.readFileSync(cvs_file_location).toString(); var csvConverter=new CSVConverter(); csvConverter.fromString(data,function(err,jsonObj){ if (err){ console.log("error msg: " + err); return null; } var

check if required JSON is valid - node

自闭症网瘾萝莉.ら 提交于 2019-12-25 02:59:32
问题 If i require a file as require('file.json'); how do i go about checking if the JSON is valid? try catch? I'm using bluebird promises so right now its just returning Promise.resolve(require('file.json')); and bluebird catches if file is not found but i also need to check the JSONs sanity. I understand you can just pass JSON.parse to a thenable if file itself is returned as a string by FS or whatever but i dont mind caching and requiring would be faster 回答1: You are looking for the Bluebird try

Bluebird Map on HashMap/Obj

若如初见. 提交于 2019-12-24 11:44:29
问题 I'm using the bluebird library to do asynchronous calls in javascript. I noticed there's a map function so I can apply a asynchronous function to each element in an array. However, I want to use this method on a object that has keys. Is there a way to do this? 回答1: There are many way to convert an object to array like this : Object.keys(yourObject).map(function(key){ return yourObject[key] }) You can use Promise.map too 来源: https://stackoverflow.com/questions/32193946/bluebird-map-on-hashmap