๋๊ตฐ๊ฐ๊ฐ ES7 async/await ํจํด์ ์ฌ์ฉํ๋ ํํฌ ํจ์์ ์ค์ ์์ ๋ฅผ ์ ๊ณตํ ์ ์๋ค๋ฉด ์ ๋ง ์ข์ ๊ฒ์ ๋๋ค.
์์ฑ๊ธฐ๋ ๋ค์๊ณผ ๊ฐ์ ์คํ ์ ์์ฑํฉ๋๋ค.
// Use this hook to manipulate incoming or outgoing data.
// For more information on hooks see: http://docs.feathersjs.com/api/hooks.html
module.exports = function (options = {}) { // eslint-disable-line no-unused-vars
return function myHook (hook) {
// Hooks can either return nothing or a promise
// that resolves with the `hook` object for asynchronous operations
return Promise.resolve(hook);
};
};
๋ค์๊ณผ ๊ฐ์ด ๋๊ธฐ ๊ฐ๋ฅํ ์ ํ๋ฅผ ๊ฑธ ๋ฐฉ๋ฒ์ ์ฐพ์ง ๋ชปํ์ต๋๋ค.
response = await request('https://whatever.com')
myHook ๋ด๋ถ. ๋์์ ์ฃผ์๋ฉด ๊ฐ์ฌํ๊ฒ ์ต๋๋ค!
return function myHook
๋๊ธฐํํ๊ธฐ๋ง ํ๋ฉด ๋ฉ๋๋ค.
// Use this hook to manipulate incoming or outgoing data.
// For more information on hooks see: http://docs.feathersjs.com/api/hooks.html
module.exports = function (options = {}) { // eslint-disable-line no-unused-vars
return async function myHook (hook) {
const body = await request('https://whatever.com');
return hook;
};
};
๋๋ ์ค๋ ์์นจ์ ์ค์ ๋ก ๋ช ๊ฐ์ง ์๋ฅผ ์ถ๊ฐํ๋ ค๊ณ ์๊ฐํ์ต๋๋ค.
์ ๋ง ๊ฐ์ฌํฉ๋๋ค! ๋ด ์ด๋ฆฌ์์ ์ค์๋ฅผ ์ ๊ฑฐํ๊ณ ๊ทํ์ ์๋ฅผ ์ฌ์ฉํ๋ฉด ๋ชจ๋ ๊ฒ์ด ์๋ฒฝํ๊ฒ ์๋ํฉ๋๋ค! async/await๊ฐ ์ฝ๋๋ฅผ ํจ์ฌ ๋ ์ฝ๊ธฐ ์ฝ๊ฒ ๋ง๋ค๊ธฐ ๋๋ฌธ์ ๋ฌธ์์ ์์ ๋ฅผ ์ถ๊ฐํ๋ ๊ฒ์ด ์ข์ต๋๋ค.
์ด ๋ฌธ์ ๋ ๋ซํ ํ ์ต๊ทผ ํ๋์ด ์์๊ธฐ ๋๋ฌธ์ ์๋์ผ๋ก ์ ๊ฒผ์ต๋๋ค. ๊ด๋ จ ๋ฒ๊ทธ์ ๋ํ ์ด ๋ฌธ์ ์ ๋ํ ๋งํฌ๊ฐ ์๋ ์ ๋ฌธ์ ๋ฅผ ์ฌ์ญ์์ค.
๊ฐ์ฅ ์ ์ฉํ ๋๊ธ
๋๋ ์ค๋ ์์นจ์ ์ค์ ๋ก ๋ช ๊ฐ์ง ์๋ฅผ ์ถ๊ฐํ๋ ค๊ณ ์๊ฐํ์ต๋๋ค.