我有以下2个URL的一些模拟数据:
1. Get the list of users from 'https://myapp.com/authors'.
2. Get the list of Books from 'https://myapp.com/books'.
Run Code Online (Sandbox Code Playgroud)
现在我的任务是按名称对Books排序,并将排序后的列表以mysortedbooks.json
JSON 格式写入文件
然后,我必须创建一个具有books属性的作者数组,其中包含该作者的所有书籍。
如果作者没有书籍,则此数组应为空。在这种情况下,无需排序,数据应authorBooks.json
以JSON 格式存储在文件中。
现在,我必须返回一个承诺,上述步骤完成后即可解决。例如,我应该saveToFile
在以下代码中返回最终调用。
const fs = require('fs');
function getFromURL(url) {
switch (url) {
case 'https://myapp.com/authors':
return Promise.resolve([
{ name: "Chinua Achebe", id: "1" },
{ name: "Hans Christian Andersen", id: "2" },
{ name: "Dante Alighieri", id: "3" },
]);
case 'https://myapp.com/books':
return Promise.resolve([
{ name: "Things Fall Apart", authorId: "1" },
{ name: …
Run Code Online (Sandbox Code Playgroud)