I am trying to use AWS Javascript SDK for accessing S3. I am using the Promises with 'Q' library. However it is not working.
I have set the Q dependency also.
var Q = require('q');
AWS.config.setPromisesDependency(Q);
Here's the code I have:
var listBucketPromise = s3.listBuckets().promise();
listBucketPromise.then(
function (response) {
console.log(" response = " + response );
} ,
function (error) {
console.log(" error = " + error);
}
);
This shows on console :
response = function (resolve, reject) {
self.on('complete', function(resp) {
if (resp.error) {
reject(resp.error);
} else {
// define $response property so that it is not enumberable
// this prevents circular reference errors when stringifying the JSON object
resolve(Object.defineProperty(
resp.data || {},
'$response',
{value: resp}
));
}
});
self.runTo();
}
I have a valid s3 client set correctly. The callback format works:
s3.listBuckets(function (err, data) {
console.log(data);
});
Why is the promise code not working ?
You need to pass a Promise constructor to setPromisesDependency. The Q function that you used does not expect a callback, when called with the typical promise executor callback it just returned a promise fulfilled with that function value.
You can use Q.Promise instead, which also is documented in this blog post's example:
// Use Q implementation of Promise
AWS.config.setPromisesDependency(require('Q').Promise);
Related
I can't grasp how promises work. So I figured I'd just jump in and try and create one to see if that helps. But the following returns an undefined value (arrTables):
app.get("/getTables", function (req, res) {
var arrTables = getTables().then(function(response) {
console.log("getTables() resolved");
console.log(arrTables.length);
console.log(arrTables[1].ID());
}, function(error) {
console.error("getTables() finished with an error");
});
});
function getTables() {
return new Promise(function(resolve, reject) {
while (mLobby.tlbCount() < LOBBY_SIZE) {
var objTable = new Table();
mLobby.addTable(objTable);
}
resolve(mLobby.tables);
});
}
new Table() references a custom class that makes an async database call. I'm trying to use promises to make sure that call resolves before I continue in the code. Can anyone point out where I've gone wrong?
Here's the console output:
getTables() resolved
undefined
(node:6580) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id:
1): TypeError: Cannot read property 'ID' of undefined
Edit to add: mLobby.tblCount starts out as 0, so it does enter the while loop.
The problem with the array variable. the GetTable method returns nothing and output of this method is stored in response variable not in arrTables variable. try to use response variable instead of arrTables
getTables().then(function(response) {
var arrTables = response //Added
console.log("getTables() resolved");
console.log(arrTables.length);
console.log(arrTables[1].ID);
}, function(error) {
console.error("getTables() finished with an error");
});
Adapting to the control flow of Promises can take some getting used to.
You're close! But...
var arrTables = getTables().then(function(response) {
console.log("getTables() resolved");
console.log(arrTables.length); ...
is a variable declaration.
This is analogous to writing var a = a. You cannot access arrTables in the declaration of arrTables because it hasn't been declared yet!
The anonymous function you are passing to .then() (where you erroneously try to access properties of the the, at the time, undefined variable arrTables) is the very same function that you call as resolve(mLobby.tables) within your promise.
The promise you return from getTables promises to pass mLobby.tables() to your anonymous function as response.
I recommend doing some practice with promises before trying to work them into a larger application.
The excellent nodeschool.io workshopper promise-it-wont-hurt was very helpful for me.
you can try following code.
app.get("/getTables", async function (req, res) {
var arrTables = await getTables()
console.log(arrTables.length);
console.log(arrTables[1].ID());
});
async function getTables() {
return new Promise(function(resolve, reject) {
try {
while (mLobby.tlbCount() < LOBBY_SIZE) {
var objTable = new Table();
mLobby.addTable(objTable);
}
resolve(mLobby.tables);
} catch (err) {
console.error("getTables() finished with an error");
reject(err)
}
});
}
hope it will work for you.
I'm new to promises and I feel like I'm doing this right however Im getting null or undefinded returned trying to return values down my promise chain (using the q lib)
Right now I am using a less sophisticated approach grabbing each returned value like so
_data.parser(user_request)
.then(function(parsedRequest) {
setOptions(parsedRequest);
}).fail(function(error) {
console.log("Error Parsing Users Request: ", error);
});
and using it in another function like so
function setOptions(parsedRequest) {
_options.setEtag(parsedRequest.fullurl) <----when I add this to a promise chain
}
like this:
_data.parser(u_req).then(function(parsedRequest) {
return _options.setEtag(parsedRequest.fullurl) <--- this returns null/undefined
}).then(function(parsedRequest) {
console.log("parsedRequest: ", parsedRequest);
return _options.setHeaders(parsedRequest.fullurl, etag)
}).then(function(headers) {
where the function its getting hung up on looks like the following
setEtag: function(fullurl, callback) {
console.log("fullurl: ", fullurl); <--- logs the path ok
var deferred = q.defer();
if(fullurl) {
etagRef.child(fullurl).child('id').once("value", function(snapshot) {
deferred.resolve(snapshot.val()); <--- HERE ITS RESOLVING BEFORE ITS DEFINED
})
} else {
deferred.reject(new Error("Could not create etag "));
}
deferred.promise.nodeify(callback);
return deferred.promise;
}
what would case this line:
deferred.resolve(snapshot.val());
to return undefined when I use the .then() chain and not when Im calling it to another function?
I have the following nodeJS code.I need to get the machines for each service from the redis database. I am using the 'q' library to simplify the callback problem. However I do not get the output.
I am new to node/callbacks/q. Where is my mistake in the code?
I have a controller.js file with the following code
function getMachines(services) {
var machines = Q.fcall(function() {});
services.forEach(function(service) {
var value = function() {
var deferred = Q.defer();
redisDB.readfromRedis(service, function(result) {
deferred.resolve(result);
});
return deferred.promise;
}
});
return machines;
}
testController.js(calling the getMachines function from the controller.js )
var services = ['dashDb22', 'service1', 'service2', 'service3']
var output = controller.getMachines(services)
console.log(output);
RedisDb.js
function readfromRedis(key, callback) {
client.smembers(key, function(error, value) {
if (error) {
throw error;
}
console.log('VALUE IS: = ' + value);
callback(value);
});
}
Your getMachines() doesn't do much, machines is useless and inside your forEach(), you're storing a function you never execute. Your code being simple, you don't really need to use Q, nodejs has a native Promise support.
function getMachines(services) {
// create an array of promises
var myPromises = services.map(function (service) {
// for each service, create a Promise
return new Promise(function (resolve, reject) {
redisDB.readfromRedis(service, function (result) {
resolve(result);
});
});
})
// takes an array of promises and returns a promise for when they've all
// fulfilled (completed successfully) with the values as the result
return Promise.all(myPromises);
}
getMachines(services).then(function (machines) {
// use machines here
});
You could also make readfromRedis() a promise to make it simpler to use.
I'm trying to get my head around promises in JavaScript. I feel like I know what a promise is. However, I don't understand how to use them. In an attempt to learn, I decided to query a database from Node.js. In my code, I have one JavaScript file called test.js. Test.js looks like this:
Test.js
'use strict';
module.exports = function (app) {
var customerService = require('customer.js')(app);
var getCustomerTest = function() {
customerService.getCustomer(1).then(
function (customer) { console.log(customer); },
function (error) { console.log(error); }
);
};
};
Customer.js
'use strict';
module.exports = function(app) {
var _ = require('lodash');
var db = require('db');
return {
getCustomer: function(customerID) {
try {
console.log('querying the database...');
var database = db.connect(CONNECTION_STRING);
database.query('select * from customers where [ID]="' + customerID + '", function(error, result, response) {
if (error) {
// trigger promise error
} else {
// This throws an exception because displayMessage can't be found.
this.displayMessage('Success');
// trigger promise success
}
});
} catch (ex) {
// trigger promise error
}
},
displayMessage: function(message) {
console.log(new Date() + ' - ' + message);
}
};
};
I'm struggling trying to setup the promise in getCustomer. Especially since the call to the database call happens asynchronously. I feel like my call to customerService.getCustomer is the correct approach. However, once inside of getCustomer, I have two issues:
How do I setup / return my promise?
Why can't I call displayMessage after the database query is done? How do I do this?
Thank you JavaScript whiz!
"Why can't I call displayMessage after the database query is done"
You are getting the error because this is not referencing the object that contains getCustomer and displayMessage. This is because you are in a callback function and the context has changed.
You need to save a reference to the correct context and use that to access displayMessage
getCustomer: function(customerID) {
//saving the context
var that = this;
try {
...
database.query('select * from customers where [ID]="' + customerID + '", function(error, result, response) {
...
// Now use it here to call displayMessage
that.displayMessage('Success');
...
}
});
} catch (ex) {
...
}
},
"How do I setup / return my promise?"
You will need a promise library (unless you plan to make your own). For this I will show the use of the q library
There are a couple ways of doing this, but I will show the use of deferreds.
The basic process is:
Create a deferred object in a function that will call an async function/method.
Return the promise object
Set the appropriate callbacks for then/fail/fin etc on the promise object.
In the callback of the async function resolve or reject the deferred, passing any arguments that will be needed in the callbacks.
The appropriate callbacks that were set in step 2 will then be called in order.
Code
var Q = require("q");
...
getCustomer:{
var deferred = Q.defer(),
database = db.connect(CONNECTION_STRING);
database.query("some query", function(error, result, response) {
if(error){
//Anything passed will be passed to any fail callbacks
deferred.reject(error);
} else {
//Anything passed will be passed to any success callbacks
deferred.resolve(response);
}
});
//Return the promise
return deferred.promise;
}
...
customerService.getCustomer(1).then(function (customer) {
console.log(customer);
}).fail(function (error) {
console.log(error);
}).done();
The q library has quite a few helpful api functions. Some help with getting promises with Node async functions. Read the Adapting Node section of the readme to see how it is done.
JSFiddle Demo demonstrating in browser use of q
I think this is a really stupid question but I'm having a hard time wrapping my head around promises.
I'm using Q (for nodejs) to sync up a couple of async functions.
This works like a charm.
var first = function () {
var d = Q.defer();
fs.readdir(path,function(err,files){
if(err) console.log(err);
d.resolve(files);
});
return d.promise;
};
var second = function (files) {
var list = new Array;
files.forEach(function(value, index){
var d = Q.defer();
console.log('looking for item in db', value);
db.query(
'SELECT * FROM test WHERE local_name =? ', [value],{
local_name : String,
},
function(rows) {
if (typeof rows !== 'undefined' && rows.length > 0){
console.log('found item!', rows[0].local_name);
d.resolve(rows[0]);
} else {
var itemRequest = value;
getItemData(itemRequest);
}
}
);
list.push(d.promise);
});
return Q.all(list);
};
first()
.then(second)
.done(function(list){
res.send(list);
});
The problem I have is with this little function:
getItemData(itemRequest)
This function is filled with several of callbacks. The promise chain runs through the function just fine but ignores all the callbacks I use ( eg several XHR calls I make in the function).
A simplified version of the function looks like this (just to give you an idea):
function getItemData(itemRequest){
helper.xhrCall("call", function(response) {
var requestResponse = JSON.parse(response)
, requestInitialDetails = requestResponse.results[0];
downloadCache(requestInitialDetails,function(image) {
image = localImageDir+requestInitialDetails.image;
helper.xhrCall("call2", function(response) {
writeData(item,image,type, function(){
loadData(item);
});
});
} else {
writeData(item,image,type, function(){
loadData(item);
});
}
});
});
The xhr function I use looks like this:
xhrCall: function (url,callback) {
var request = require("request")
, colors = require('colors');
request({
url: url,
headers: {"Accept": "application/json"},
method: "GET"
}, function (error, response, body) {
if(!error){
callback(body);
}else{
console.log('Helper: XHR Error',error .red);
}
});
}
So my questions:
Can I leave the function unaltered and use the callbacks that are in place ánd the promise chain?
Or do I have to rewrite the function to use promises for the XHR?
And if so, How can I best write my promise chain? Should I reject the initial promise in the forEach?
Again, sorry if this is a really stupid question but I don't know what the right course of action is here.
Thanks!
[EDIT] Q.nfcall, I don't get it
So I've been looking into Q.nfcall which allows me to use node callbacks. Bu I just don't understand exacly how this works.
Could someone give a simple example how I would go about using it for a function with several async xhr calls?
I tried this but as you can see I don't really understand what I'm doing:
var second = Q.nfcall(second);
function second (files) {
[EDIT 2]
This is the final funcction in my getitemdata function callback chain. This function basically does the same as the function 'second' but I push the result directly and then return the promise. This works as stated, but without all the additional callback data, because it does not wait for the callbacks to return with any data.
function loadData(item) {
var d = Q.defer();
db.query(
'SELECT * FROM test WHERE local_name =? ', [item],{
local_name : String,
},
function(rows) {
if (typeof rows !== 'undefined' && rows.length > 0){
list.push(d.promise);
}
}
);
});
return Q.all(list);
};
Your answer is not really clear after your second edit.
First, on your orignal question, your getItemData has no influence on the promise chain.
You could change you the function's call signature and pass your deferred promise like so.
getItemData(itemRequest, d)
and pass this deferred promises all the way to your xhrCall and resolve there.
I would re-write your whole implementation and make sure all your functions return promises instead.
Many consider deferred promises as an anti-pattern. So I use use the Promise API defined in harmony (the next javascript)
After said that, I would re-implement your original code like so (I've not tested)
var Promise = Promise || require('es6-promise').Promise // a polyfill
;
function errHandler (err){
throw err
}
function makeQuery () {
var queryStr = 'SELECT * FROM test WHERE local_name =? '
, queryOpt = {local_name: String}
;
console.log('looking for item in db', value)
return new Promise(function(resolve, reject){
db.query(queryStr, [value], queryOpt, function(rows) {
if (typeof rows !== 'undefined' && rows.length > 0){
console.log('found item!', rows[0].local_name);
resolve(rows[0]);
} else {
// note that it returns a promise now.
getItemData(value).then(resolve).catch(errHandler)
}
})
})
}
function first () {
return new Promise(function(resolve, reject){
fs.readdir(path, function(err, files){
if (err) return reject(err)
resolve(files)
})
})
}
function second (files) {
return Promise.all(files.map(function(value){
return makeQuery(value)
});
}
first()
.then(second)
.then(res.send)
.catch(errHandler)
Note that there is no done method on the Promise API.
One down side of the new Promise API is error handling. Take a look at bluebird.
It is a robust promise library which is compatible with the new promise API and has many of the Q helper functions.
As far as I can tell, you need to return a promise from getItemData. Use Q.defer() as you do in second(), and resolve it when the callbacks complete with the data. You can then push that into list.
To save code, you can use Q.nfcall to immediately call a node-style-callback function, and return a promise instead. See the example in the API docs: https://github.com/kriskowal/q/wiki/API-Reference#qnfcallfunc-args