I have tried to used the promise.all - that allows you to wait for multiple promises and return a single result. However, it doesn’t return any values. Here, is my code
var promise1 = dynamoDb.get(params1).promise().then(function (response) {
return response.Item.stopTimestamp;
});
var promise2 = dynamoDb.get(params2).promise().then(function (response) {
return response.Item.startTimestamp;
});
Promise.all([promise1, promise2]).then(function(values){
return values;
});
This returns:-
{}
It can return either “promise1” or “promise2”, but, not both.
The code below works which returns promise1.
var promise1 = dynamoDb.get(params1).promise().then(function (response) {
return response.Item.stopTimestamp;
});
return promise1;
Which returns:-
1534246486047
Have you tried this?
Promise.all([promise1, promise2]).then(function(values) {
// Do what you want with the values here, like:
console.log(values);
});
Just returning values
pretty much does nothing.
The thing is I need to return values (from promise1
and promise2
to the GET API).
When I only try to return values from promise1
, it works:-
var promise1 = dynamoDb.get(params1).promise().then(function (response) {
return response.Item.stopTimestamp;
});
return promise1;
which returns
15342486888098
However, return from promise1
and promise2
using Promise.all is not working.
The resolved values should be inside values
in Promise.all()
as an array. If you need to do something with those resolved values, you’ll do it right inside the callback for Promise.all().then()
Promise.all([promise1, promise2]).then(function(values) {
// suppose I need to pass them to some other API that requires start and stop timestamps
someFictionalAPI.get({
stop: values[0],
start: values[1],
});
});
Sorry I’m not aware of the API you’re using, but it should be along those lines
This is the api I have created using claudiajs deployement tool. Here is the code:-
api.get('gettime', function(request, response) {
var token = request.headers['x-access-token'];
var decoded = jwt.verify(token, 'heymanwhatsup');
if(!token) {
throw new ApiError('message: No token provided');
}
var params1 = {
TableName: 'Stop-Timestamp',
Key: {
stopId: token
}
}
var params2 = {
TableName: 'Start-Timestamp',
Key: {
startId: token
}
}
var promise1 = dynamoDb.get(params1).promise().then(function (response) {
return response.Item.stopTimestamp;
});
var promise2 = dynamoDb.get(params2).promise().then(function (response) {
return response.Item.startTimestamp;
});
//return promise1;
Promise.all([promise1, promise2]).then(function(values) {
console.log(values);
});
}, { success: 200, error: 401});
If I only return promise1
, it returns the value. However, at the moment, Promise.all
is not returning any values
So should the callback for api.get
return a value? That’s as much as I can glean from your code and the documentation.
So maybe you should return values
after all, then return the entire Promise.all
call?
return Promise.all([promise1, promise2]).then(function(values) {
return values;
});
Thank you! Now it returns both the values from promise1
and promise2
.