小编Neo*_*eoN的帖子

如何在序列异步函数中处理数组

我正在尝试制作一个包含 Shopify 订单和图片链接的表格。但异步循环有问题。每次我运行代码时,控制台中的顺序都会以错误的顺序出现,例如 998、996、1000... 应该是 1000、999、998...\n我尝试在不同的位置添加 async 关键字,并包装一个settimeout 函数中的函数等等,但没有运气。\n如何使订单列表按正确的顺序?\n提前谢谢您。

\n\n

\r\n
\r\n
const\xc2\xa0{google}\xc2\xa0=\xc2\xa0require(\'googleapis\');\r\nconst\xc2\xa0keys\xc2\xa0=\xc2\xa0require(\'./keys.json\');\r\nconst Shopify = require(\'shopify-api-node\');\r\nconst\xc2\xa0client\xc2\xa0=\xc2\xa0new\xc2\xa0google.auth.JWT(\r\n    keys.client_email,\r\n    null,\r\n    keys.private_key,\r\n    [\'https://www.googleapis.com/auth/spreadsheets\',\r\n    \'https://www.googleapis.com/auth/drive.metadata.readonly\']\r\n);\r\nconst shopify = new Shopify({\r\n  shopName: \'name.myshopify.com\',\r\n  apiKey: \'key\',\r\n  password: \'pas\'\r\n});\r\nconst\xc2\xa0sheets\xc2\xa0=\xc2\xa0google.sheets({version:\xc2\xa0\'v4\',\xc2\xa0auth:\xc2\xa0client});\r\nconst\xc2\xa0drive\xc2\xa0=\xc2\xa0google.drive({version:\xc2\xa0\'v3\',\xc2\xa0auth:\xc2\xa0client});\r\n\r\nfunction getLink(){\r\n    client.authorize(()=> gsrun());\r\n}\r\n\r\nasync function runDrive(ord, sku){\r\n    const resDrive = await drive.files.list({\r\n    pageSize:\xc2\xa01,\r\n    q: `name contains "sku"`,\r\n    spaces:\xc2\xa0\'drive\',\r\n    });\r\n    const\xc2\xa0files\xc2\xa0=\xc2\xa0resDrive.data.files;\r\n    let link;\r\n    if\xc2\xa0(files.length)\xc2\xa0{\r\n            link = `https://drive.google.com/file/d/${files[0].id}/view`;\r\n    } else\xc2\xa0{\r\n    // console.log(\'No\xc2\xa0files\xc2\xa0found.\');\r\n    link = \'No\xc2\xa0files\xc2\xa0found.\';\r\n    }\r\n    console.log([ord, sku, link])\r\n}\r\n\r\nasync\xc2\xa0function\xc2\xa0gsrun(){\r\n    // Shopify - Get orders list\r\n    let orders = await shopify.order.list({ limit: 7 …
Run Code Online (Sandbox Code Playgroud)

javascript google-api node.js google-drive-api shopify-api-node

0
推荐指数
1
解决办法
1424
查看次数