gpt4 book ai didi

javascript - NodeJS 脚本不使用 async/await

转载 作者:行者123 更新时间:2023-11-30 11:14:06 24 4
gpt4 key购买 nike

无法弄清楚为什么下面的脚本无法运行。脚本很可能不会执行我想要的操作,而是使用

node ./contentful/contentful-assets.js

在终端中,它什么都不做——没有错误,没有任何记录让我开始调试。但是,如果我删除 async 它将尝试脚本并返回错误。

./contentful/contentful-assets.js

const contentful = require('contentful-management');
const iterator = require('make-iterator');
const assets = require('./assetObject.js');

async resolve => {
console.log('Creating Contentful client');
const client = contentful.createClient({
accessToken: 'token',
logHandler: (level, data) => console.log(`${level} | ${data}`)
});

const iterableAssets = iterator(assets);
const space = await client.getSpace('space');
const environment = await space.getEnvironment('enviroment');

const cmsAssets = [];

const assetProcessingTimes = [];
const inProcess = new Map();
let processedAssetsCounter = 0;

const createAndPublishSingleAsset = async ({ asset, done, index }) => {
if (done) {
if (inProcess.size > 0) return false;
return resolve(cmsAssets);
}

const start = Date.now();
const id = '' + start + Math.round(Math.random() * 100);
inProcess.set(id, true);
let cmsAsset;

try {
cmsAsset = await environment.createAssetWithId(asset.postId, {
fields: {
title: {
'en-US': asset.title
},
description: {
'en-US': asset.description
},
file: {
'en-US': {
contentType: 'image/jpg',
fileName: asset.filename,
upload: asset.link
}
}
}
});
} catch (e) {
console.log(`Asset "${asset.title}" failed to create, retrying...`);
createAndPublishSingleAsset({
asset,
done,
index
});
}
try {
const processedCMSAsset = await cmsAsset.processForAllLocales();
const publishedCMSAsset = await processedCMSAsset.publish();

cmsAssets.push(publishedCMSAsset);
assetProcessingTimes.push((Date.now() - start) / 1000);
inProcess.clear(id);
const eta = Math.floor(
assetProcessingTimes.reduce((a, b) => a + b, 0) /
assetProcessingTimes.length *
(assets.length - index) /
60
);
processedAssetsCounter += 1;
console.log(
`Processed asset ${processedAssetsCounter}/${assets.length} - eta: ${eta}m`
);
createAndPublishSingleAsset(iterableAssets.next());
} catch (e) {
console.log(`Asset "${asset.title}" failed to process, retrying...`);
await cmsAsset.delete();
createAndPublishSingleAsset({
asset,
done,
index
});
}
};
console.log('Starting to create assets');
createAndPublishSingleAsset(iterableAssets.next());
createAndPublishSingleAsset(iterableAssets.next());
createAndPublishSingleAsset(iterableAssets.next());
};

assetObject.js

[
{
link: 'https://example.com/example1.jpg',
title: 'Example 1',
description: 'Description of example 1',
postId: '1234567890',
filename: 'example1.jpeg'
}, ... // Many more
]

我在这里错过了什么?

最佳答案

我担心你没有调用函数,你可以试试下面的方法吗?

const contentful = require('contentful-management');
const iterator = require('make-iterator');
const assets = require('./assetObject.js');

const doWork = async resolve => {
console.log('Creating Contentful client');
...
}

doWork();

您只是声明了一个异步函数并执行所有已定义的代码,但实际上并没有调用它。

关于javascript - NodeJS 脚本不使用 async/await,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52346255/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com