我有node.js的這個問題,我在啓動我的應用程序時使用節點--trace-sync-io,當我使用請求模塊和請求承諾模塊對我的server.js文件進行api調用時,它仍然警告我正在使用Sync API並指向我的request.promise調用的開始。如何使用Node.js進行異步API調用?
var request = require("request");
var rp = require("request-promise");
var options = {
uri: url,
json: true
};
rp(options).then(function (data){
//then I do something with data here
}.catch(function(err){
//catch errors here
})
所以當我開始用節點命令node --trace-sync-io server.js
我得到WARNING: Detected use of sync API
和堆棧跟蹤
(node:17212) WARNING: Detected use of sync API
at rng (mypath\node_modules\uuid\lib\rng.js:7:10)
at v4 (mypath\node_modules\uuid\v4.js:13:52)
at Multipart (mypath\node_modules\request\lib\multipart.js:11:19)
at Request (mypath\node_modules\request\request.js:127:21)
at request (mypath\node_modules\request\index.js:54:10)
at mypath\server.js:333:8
at emitOne (events.js:96:13)
at emit (events.js:188:7)
at Query.handleReadyForQuery (mypath\node_modules\pg\lib\query.js:126:8)
指向的地方,我開始呼叫RP行(options.then(功能(數據)))
它指向[此功能](https://github.com/kelektiv/node-uuid/blob/3b218806aa82e76c7aaa6c9ad62e4d5abe2de4e3/lib/rng.js#L6-L8),它調用同步版本的'crypto.randomBytes '。你不能修補'request'和'uuid',但我沒有看到實際的問題。 – robertklep
@robertklep爲什麼請求模塊調用類似的東西?我認爲這個問題是我讀到使用sync api和node.js會降低整個站點的速度,因爲服務器在使用sync api時無法處理任何其他內容。但是我不知道這個特別的東西有什麼樣的影響? – Quartal
'request'將它用於多部分請求(UUID用作分隔符),即使在您未發佈任何多部分數據時也是如此。但是沒什麼大不了的,我的筆記本電腦每秒可以產生大約300K UUIDv4,因此每個筆記本大約需要3.3微秒。這不會是顯而易見的。 – robertklep