return value of promise - javascript

Im new to promises and I'm trying to to return the value of a promise like mongoose does but using mongoskin and bluebird. This works with mongoose return User.find().then(users => users). This will return a list of users not a promise in apollo-server resolvers.
I've tried promise generator and async but with no luck. From what I've read a promise always returns a promise so no idea how mongoose is returning a value.
mongodb.js
import Promise from 'bluebird';
import mongoskin from 'mongoskin';
Object.keys(mongoskin).forEach(function (key) {
var value = mongoskin[key];
if (typeof value === 'function') {
Promise.promisifyAll(value);
Promise.promisifyAll(value.prototype);
}
});
Promise.promisifyAll(mongoskin);
export default {
connect (uri) {
return mongoskin.db(uri, {native_parser:true});
}
};
users.js
import mongodb from '../../databases/mongodb';
export default class User {
constructor () {
this.db = mongodb.connect('mongodb://127.0.0.1:27017/test', {native_parser:true});
this.collection = this.db.collection('users');
}
find (query = {}, options = {}) {
const findAsync = () => {
return Promise.resolve().then(() => {
return this.collection.findAsync(query, options);
})
.then((xx) => {
xx.toArray((err, items) => {
if (err) return err;
return items;
});
});
};
async function getData () {
let foo = await findAsync();
return foo;
}
return getData();
}
}
const user = new User();
function bar () {
return user.find().then(x => console.log(x));
}
console.log(bar());

Your code seems overly complicated. I think what you want is this (I didn't promisify mongoskin because it's not very well suited for that; see below):
export default class User {
constructor() {
this.db = mongoskin.connect(...);
this.collection = this.db.collection('users');
}
find (query = {}, options = {}) {
return new Promise((resolve, reject) => {
this.collection.find(query, options).toArray((err, items) => {
if (err) return reject(err);
resolve(items);
});
});
}
}
I have to say that Mongoskin feels pretty outdated. It's a skin on top of the official mongodb driver, which is pretty decent nowadays (it supports promises out-of-the-box, for one, something that Mongoskin doesn't propagate).
If you do want to promisify, then I should say that the following is a promise anti-pattern:
return Promise.resolve().then(() => {
return this.collection.findAsync(query, options);
}).then(...)
You can rewrite it to this:
return this.collection.findAsync(query, options).then(...);
However, the toArray() makes things difficult again, because for that you do need to create a new promise, so the code becomes something like this:
return this.collection.findAsync(query, options).then((cursor) => {
return new Promise((resolve, reject) => {
cursor.toArray((err, items) => {
if (err) return reject(err);
resolve(items);
});
});
});
Which doesn't look very pretty at all, hence my choice to not promisify and just use callbacks in this case (although I'm sure that Bluebird has some nice tools that may the above code easier to look at, but still...).

Related

Nodejs promise pending

I'm trying to make an constructor for multiple Redis connections, so i've started to try something.
I'm only getting back from has Promise { }, but if I do an console.log before the return I'm getting the real Value.
EDIT: Tried without async/await still don't work.
app.js
const rBredis = require("./redis");
const redis = new rBredis();
console.log(redis.has("kek"));
redis.js
const Redis = require("ioredis");
class BasicRedis {
constructor() {
// TODO
};
redis = new Redis();
async has(id) {
return await this.redis.exists(id)
.then( exists => {
// console.log(exists); works 0
return exists; // works not Promise { <pending> }
});
};
}
module.exports = BasicRedis;
I don't understand your question completely but I see a problem here.
You need to brush up your knowledge of Promises and Async await. You either use async
await or Promises (.then) syntax to make it work properly.
redis.js
class BasicRedis {
constructor() {
// TODO
};
redis = new Redis();
// You can either do it like this
has(id) {
return new Promise((res, rej) => {
this.redis.exists(id)
.then( exists => {
res(exists)
}).catch(err => {
rej(err.message)
});
})
};
// Or like this
has(id) {
return this.redis.exists(id)
};
}
In both cases, you can await/.then result in your app.js
// app.js
const rBredis = require("./redis");
const redis = new rBredis();
redis.has("kek").then(res => console.log(res))
EDIT - 1
If this is something that'd take time even 1 millisecond there's no way you're going to get the value right away. You need to use either async-await or promises. Or use a callback like this
redis.js
class BasicRedis {
constructor() {
// TODO
};
redis = new Redis();
has(id, callback) {
this.redis.exists(id)
.then( exists => {
callback(exists)
}).catch(err => {
callback(err.message)
});
};
}
app.js
const rBredis = require("./redis");
const redis = new rBredis();
redis.has("kek", (res) => console.log(res))
Here's reference to Promises MDN and Async Await MDN
Hope it helps.

How do I access promise callback value outside of the function?

It is to my understanding that callback functions are asynchronous and cannot return a value like regular functions. Upon reading about promises, I thought I grasped a good understanding about them, that they are basically an enhanced version of callbacks that allows returning a value like asynchronous function. In my getConnections method, I am attempting to call the find() function on my database through mongoose, and I am attempting to grab this array of objects and send it to the views.
var test = new Promise((resolve, reject) => {
Database.find().then(list => {
resolve(list);
}).catch(err=> {
return reject(err);
})
})
console.log(test)
When I attempt to log to the console outside of the promise function, I get Promise { _U: 0, _V: 0, _W: null, _X: null }
I don't think this is functioning correctly, and I thought I utilized promises correctly. Could anyone point me in the right direction on how to return this array of objects outside of the callback function?
You can simply add await before the promise declaration.
i.e.
var test = await new Promise...
The thing is that when you write a function like this:
const getData = async () => { const response = await fetch(someUrl, {}, {}); return response;}
Then you also need to await that function when you call it. Like this:
const setData = async () => { const dataToSet = await getData(); }
let test = new Promise((resolve, reject) => {
Database.find().then(list => {
resolve(list);
}).catch(err=> {
return reject(err);
})
})
test
.then(result=>console.log(result))
Should solve your problem.
var someValue;
var test = await new Promise((resolve, reject) => {
Database.find().then(list => {
resolve(list);
}).catch(err=> {
return reject(err);
})
}).then(res => {
someValue=res;
})
console.log(someValue);

Data initializing with promises

I really struggle with data initializing and promises. I am using Ionic 3 with Angular and Ionic storage, but my question is mainly related to how promises operate.
Basically I would like to achieve the following:
when my app starts it should use the local storage collection
if the local storage collection does not exist or is empty, create a new one with http
if the http fails, create a collection with local data.
My solution so far:
getNewsItems():Promise<any> {
return this.storage.get(this.newsKey).then((data) => {
if(data == null)
{
return (this.buildNewsItemsViaHttp());
} else {
return (data);
}
});
}
private buildNewsItemsViaHttp(){
return new Promise(resolve => {
this.http.get('some/url/to/fetch/data')
.subscribe(
data => {
this.newsCollection = data;
this.storage.set(this.newsKey, this.newsCollection);
resolve(this.newsCollection);
},
(err) => {
resolve (this.buildNewsItemsViaLocalJSON());
}
);
});
}
private buildNewsItemsViaLocalJSON() {
return new Promise(resolve => {
this.http.get('assets/data/newsCollectionLocal.json')
.subscribe(
data => {
this.newsCollection = data;
this.storage.set(this.newsKey, this.newsCollection);
resolve(this.newsCollection);
},
(err) => {
console.log(err);
}
);
});}
I don't like some parts of it, for example returning a promise inside a promise - is this actually an issue?
Thanks in advance
A clean solution could be using async\await methods:
async buildNewsItemsViaHttp(){
return await this.http.get()....
}
async buildNewsItemsViaLocalJSON(){
return await this.http.get()....
}
async getNewsItems(){
return await this.storage.get()...
}
async getItems(){
return ((await this.getNewsItems()) || (await this.buildNewsItemsViaLocalJSON()) || (await this.buildNewsItemsViaHttp()));
}
usage:
const items = await this.getItems();
You can optimize the resources, cache them and return them in each function.
Example:
async buildNewsItemsViaHttp(){
let result = cache.get(); // todo
if(!result){
result = await this.http.get()...
cache.set(result)
}
return result;
}

How to flatten a Promise within a Promise?

I have the following 2 functions, each returns a Promise:
const getToken = () => {
return new Promise((resolve, reject) => {
fs.readFile('token.txt', (err, data) => {
if (err) { return reject(err) }
if (!tokenIsExpired(data.token)) {
return resolve(data.token)
} else {
return requestNewToken()
}
})
})
}
const requestNewToken = () => {
return new Promise((resolve, reject) => {
restClient.get(url, (data, res) => {
fs.writeFile('tokenfile.txt', data.token, err => {
resolve(data.token)
})
})
})
}
function1()
.then(value => {
console.log('taco')
})
.catch(err => {
console.log(err)
})
So function1 runs, and (depending on some condition), it sometimes returns function2, which is returning another Promise. In this code, when function2 is called, the console.log('taco') never runs. Why is this? I thought that if you return a Promise from within a Promise, the resolved value of the nested Promise is what is resolved at the top level.
In order for me to get this to work, I have to do this:
const getToken = () => {
return new Promise((resolve, reject) => {
if (!tokenIsExpired()) {
return resolve(getToken())
} else {
return requestNewToken ()
.then(value => {
resolve(value)
})
}
})
}
That works, but it seems like I'm doing something wrong. It seems like there should be a more elegant way to handle/structure this.
You're right that promises auto-unwrap, but in this case you're returning from inside a promise constructor, which is ignored, you need to invoke either resolve or reject instead of using return. I think this might be the solution you're looking for:
const function1 = () => {
return new Promise((resolve, reject) => {
if (someCondition) {
resolve('foobar')
} else {
resolve(function2());
}
})
}
Inside a promise constructor, you need to call resolve or reject, which are equivalent to using return or throw from inside a then callback.
If you find this distinction confusing (I do), you should avoid the promise constructor entirely by just beginning a new chain with Promise.resolve, like this:
const function1 = () => {
return Promise.resolve().then(() => {
if (someCondition) {
return 'foobar';
} else {
return function2();
}
})
}
const function2 = () => {
return new Promise((resolve, reject) => {
resolve('hello world')
})
}
someCondition = false;
function1()
.then(value => {
console.log(value)
})
With your updated code, I recommend using a library to wrap APIs, rather than accessing the promise constructor yourself. For example, using bluebird's promisify:
const bluebird = require('bluebird');
const readFile = bluebird.promisify(fs.readFile);
const writeFile = bluebird.promisify(fs.writeFile);
const getUrl = bluebird.promisify(restClient.get, {multiArgs:true});
const getToken = () => {
return readFile('token.txt')
.then((data) => {
if(!tokenIsExpired(data.token)) {
return data.token;
} else {
return requestNewToken();
}
});
};
const requestNewToken = () => {
return getUrl(url)
.then(([data, res]) => {
return writeFile('tokenFile.txt', data.token)
.then(() => data.token);
});
};
I've remained faithful to your source code, but I'll note there may be a bug to do with writing data.token, and later trying to read the token property in that file.
Why use a library instead of the Promise constructor?
It allows you to write code which deals only with promises, which is (hopefully) easier to understand
You can be confident that callback APIs are correctly converted without losing errors. For example, if your tokenIsExpired function throws an error, using your promise constructor code, it would be lost. You would need to wrap all of your inner callback code in try {} catch(e) {reject(e)}, which is a hassle and easily forgotten.

is there any method in bluebird which works like async.waterfall

I am doing something like this in which first function is dependent dependent on second .
let findOrg = () => {
return new Promise((resolve, reject) => {
db.organization.find({
where: data
})
.then(org => {
return resolve(org);
}).catch(err => {
reject(err);
});
}); };
let createOrg = org => {
return new Promise((resolve, reject) => {
if (org) {
return resolve(org);
}
db.organization.build(data)
.save()
.then((org) => {
return resolve(org);
}).catch(err => {
reject(err);
});
}); };
findOrg()
.then(org => { //going to find org
return createOrg(org); //then going to make org
}).then(newOrg => {
res.data(mapper.toModel(newOrg)); //then mapping
}).catch(err => {
return res.failure(err);
});
in both above function findOrg and createOrg new promise (ES6) are created
My ques is --
1. how we can solve this in Bluebird promise library (in sequence if one function is dependent on other) like
async.waterfall([
function(){},
function(){}],
function(){})
here 2 promises are created .is there any way i
You can use bluebird's Promise.reduce or create your own waterfall (chain) function like this as alternative.
But: your code has unnecessary overhead in using the promise constructor antipattern: you can write it without using new Promise, and with .bind you also avoid creating inline functions explicitly, ... like this:
let findOrg = () => db.organization.find({where: data});
let createOrg = org => org || db.organization.build(data).save();
findOrg()
.then(createOrg)
.then(mapper.toModel.bind(mapper))
.then(res.data.bind(res))
.catch(res.failure.bind(res));
This is quite clean in my opinion.

Categories

Resources