{  var user = {    id:id,    name:..."/>
溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

nodejs漸入佳境[13]-node回調操作

發(fā)布時間:2020-07-23 22:49:58 來源:網絡 閱讀:227 作者:jonson_jackson 欄目:開發(fā)技術

自定義回調函數(shù)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//自定義函數(shù),包含回調函數(shù)
var getUser=(id,callback)=>{
 var user = {
   id:id,
   name:'jonson'
 }
 setTimeout(()=>{
   callback(user);
 },3000);
}

//調用,并且傳遞了一個回調函數(shù)
getUser(31,(userobject)=>{
 console.log(userobject);
})

執(zhí)行后輸出:

1
{ id: 31, name: 'jonson' }

天氣網站回調

1
> npm install --save request
1
2
3
4
5
6
7
8
const request = require('request');

request({
 url:'https://api.openweathermap.org/data/2.5/forecast?q=beijing,cn&appid=09ec05ac89602c9970393fe760db2bf5',
 json:true   // 將json返回結果解析為json對象
},(error,response,body)=>{
 console.log(body);
});

打印出:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
{ cod: '200',
 message: 0.0038,
 cnt: 38,
 list:
  [ { dt: 1542261600,
      main: [Object],
      weather: [Array],
      clouds: [Object],
      wind: [Object],
      rain: [Object],
      sys: [Object],
      dt_txt: '2018-11-15 06:00:00' },
    { dt: 1542272400,
      main: [Object],
      weather: [Array],
      clouds: [Object],
      wind: [Object],
      rain: [Object],
      sys: [Object],
      dt_txt: '2018-11-15 09:00:00' },

     ...

好看的格式顯示

1
2
3
4
5
6
7
8
9

const request = require('request');

request({
 url:'https://api.openweathermap.org/data/2.5/forecast?q=beijing,cn&appid=09ec05ac89602c9970393fe760db2bf5',
 json:true   // 將返回結果解析為json對象
},(error,response,body)=>{
 console.log(JSON.stringify(body,undefined,2));
});

打印出:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
{
 "cod": "200",
 "message": 0.0026,
 "cnt": 38,
 "list": [
   {
     "dt": 1542261600,
     "main": {
       "temp": 282.38,
       "temp_min": 277.973,
       "temp_max": 282.38,
       "pressure": 1017.9,
       "sea_level": 1043.48,
       "grnd_level": 1017.9,
       "humidity": 90,
       "temp_kf": 4.4
     },
     "weather": [
       {
         "id": 500,
         "main": "Rain",
         "description": "light rain",
         "icon": "10d"
       }
     ],
     "clouds": {
       "all": 92
     },
     "wind": {
       "speed": 1.71,
       "deg": 330.5
     },
     "rain": {
       "3h": 0.98
     },
     "sys": {
       "pod": "d"
     },
     "dt_txt": "2018-11-15 06:00:00"
   },

處理用戶輸入:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

const request = require('request');
const yargs = require('yargs');

const argv = yargs.options({
 a:{
   demand:true,
   alias:'address',
   describe:'Address to fetch weather for',
   string:true
 }
})
.help()
.alias('help','h')
.argv;

request({
 url:`https://samples.openweathermap.org/data/2.5/forecast?q=${argv.address},cn&appid=b6907d289e10d714a6e88b30761fae22`,
 json:true   // 將返回結果解析為json對象
},(error,response,body)=>{
 console.log(JSON.stringify(body,undefined,2));
});

測試

1
> node async.js -a xingjiang
  • 本文鏈接: https://dreamerjonson.com/2018/11/15/node-13/

  • 版權聲明: 本博客所有文章除特別聲明外,均采用 CC BY 4.0 CN協(xié)議 許可協(xié)議。轉載請注明出處!

nodejs漸入佳境[13]-node回調操作

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI