Feathers: ํ•„์š”ํ•œ ํ›„ํฌ์— ๋Œ€ํ•œ ๋น„๋™๊ธฐ/๋Œ€๊ธฐ์˜ ์˜ˆ

์— ๋งŒ๋“  2017๋…„ 09์›” 24์ผ  ยท  4์ฝ”๋ฉ˜ํŠธ  ยท  ์ถœ์ฒ˜: feathersjs/feathers

๋ˆ„๊ตฐ๊ฐ€๊ฐ€ 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 ๋‚ด๋ถ€. ๋„์›€์„ ์ฃผ์‹œ๋ฉด ๊ฐ์‚ฌํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค!

๊ฐ€์žฅ ์œ ์šฉํ•œ ๋Œ“๊ธ€

๋‚˜๋Š” ์˜ค๋Š˜ ์•„์นจ์— ์‹ค์ œ๋กœ ๋ช‡ ๊ฐ€์ง€ ์˜ˆ๋ฅผ ์ถ”๊ฐ€ํ•˜๋ ค๊ณ  ์ƒ๊ฐํ–ˆ์Šต๋‹ˆ๋‹ค.

๋ชจ๋“  4 ๋Œ“๊ธ€

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๊ฐ€ ์ฝ”๋“œ๋ฅผ ํ›จ์”ฌ ๋” ์ฝ๊ธฐ ์‰ฝ๊ฒŒ ๋งŒ๋“ค๊ธฐ ๋•Œ๋ฌธ์— ๋ฌธ์„œ์— ์˜ˆ์ œ๋ฅผ ์ถ”๊ฐ€ํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค.

์ด ๋ฌธ์ œ๋Š” ๋‹ซํžŒ ํ›„ ์ตœ๊ทผ ํ™œ๋™์ด ์—†์—ˆ๊ธฐ ๋•Œ๋ฌธ์— ์ž๋™์œผ๋กœ ์ž ๊ฒผ์Šต๋‹ˆ๋‹ค. ๊ด€๋ จ ๋ฒ„๊ทธ์— ๋Œ€ํ•œ ์ด ๋ฌธ์ œ์— ๋Œ€ํ•œ ๋งํฌ๊ฐ€ ์žˆ๋Š” ์ƒˆ ๋ฌธ์ œ๋ฅผ ์—ฌ์‹ญ์‹œ์˜ค.

์ด ํŽ˜์ด์ง€๊ฐ€ ๋„์›€์ด ๋˜์—ˆ๋‚˜์š”?
0 / 5 - 0 ๋“ฑ๊ธ‰