The Peppr app uses promises when doing DB queries, such as:
const getUser = email => {
return db.query('SELECT * FROM users WHERE email = $1', [email])
.then(user => user[0])
.catch(console.log)
}
This is possible without constructing a new Promise
because the module pg-promise has been required in their db_connection.js file. It allows us to handle the result from a DB query once it has finished, without having to use callbacks.
The Peppr app has a good example of connection promises together in their auth.js file:
//this requires the DB queries like getUser above
const queries = require('./queries');
queries
.getUser(email)
.then(user => {
//a new promise is created
return new Promise((resolve, reject) => {
if(user){
res.status(422).send({ error: 'Email is in use. Please login'});
//the user already exists, so an error is thrown using reject
reject('Email is in use. Please sign in');
}
//the user doesn't exist yet, so their password is hashed
else resolve(hashPassword(password));
})
})
.then(hash => {
return queries.addUser(name, email, hash)
})
.then (user => {
//as this is the last then, it doesn't need to be asynchronous
res.json({ token: userToken(user)});
})
.catch(console.log)
}
To turn get requests (e.g. an external API call) into a promise easily, there is the request-promise module.
const rp = require('request-promise');
rp('https://pokeapi.co/api/v2/pokemon/pikachu/')
.then(function (htmlString) {
// Process html...
})
.catch(function (err) {
// Crawling failed...
});
This also means you won't need to chunk-ify/parse the body. It comes back fully-formed and ready to be handled.
Since it is not supportes by all browser, use promise-polyfill
db_build.js:
const runDbBuild = () => {
return db.any(build)
.then(res => console.log('res', res))
.catch(e => console.error('error', e));
};
test:
test('check parent exists', (t) => {
runDbBuild().then(() => {
const email = 'k@a.com';
return checkParent(email)
}).then((queryRes) => {
t.equal(queryRes[0].case, true, 'If parent exists then check_parent should return true- promise');
t.end();
}).catch((err) => {
throw err;
})
})
or
or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up
Syntax | Example | Reference | |
---|---|---|---|
# Header | Header | 基本排版 | |
- Unordered List |
|
||
1. Ordered List |
|
||
- [ ] Todo List |
|
||
> Blockquote | Blockquote |
||
**Bold font** | Bold font | ||
*Italics font* | Italics font | ||
~~Strikethrough~~ | |||
19^th^ | 19th | ||
H~2~O | H2O | ||
++Inserted text++ | Inserted text | ||
==Marked text== | Marked text | ||
[link text](https:// "title") | Link | ||
 | Image | ||
`Code` | Code |
在筆記中貼入程式碼 | |
```javascript var i = 0; ``` |
|
||
:smile: | ![]() |
Emoji list | |
{%youtube youtube_id %} | Externals | ||
$L^aT_eX$ | LaTeX | ||
:::info This is a alert area. ::: |
This is a alert area. |
On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?
Please give us some advice and help us improve HackMD.
Do you want to remove this version name and description?
Syncing