मैं आइटम स्कैन करना चाहता हूं और डुप्लिकेट कोड का उपयोग करने से बचना चाहता हूं।
इसलिए, मैं इसके लिए अतुल्यकालिक रूप से for-of
का उपयोग करने का प्रयास कर रहा हूं।
async function checkDupl(){
const arr = new Array(10).fill(0);
let code = '';
for(const i of arr){
//generate RANDOM CODE
//for example, it would be '000001' to '000010'
code = (Math.floor(Math.random() * 10) + 1).toString().padStart(6,"0");
const params = { ... }; // it has filterExpression the code I generated randomly
await DYNAMO_DB.scan(params, (err, res) => {
if(res.Items.length === 0) {
/* no duplicate! */
return code;
}
});
}
return code;
}
console.log(checkDupl());
// it always return '';
मैंने क्या याद किया या गलत समझा?
0
kyun
30 अक्टूबर 2019, 09:18
1 उत्तर
सबसे बढ़िया उत्तर
await
बस एक प्रॉमिस (या thenable
ऑब्जेक्ट) की प्रतीक्षा कर रहा है, लेकिन आप await
का उपयोग "void" फ़ंक्शन के साथ कर रहे हैं (आप DYNAMO_DB.scan
को कॉलबैक स्टाईट फ़ंक्शन के रूप में उपयोग करते हैं)।
मेरा सुझाव है, DYNAMO_DB.scan
को प्रॉमिस स्टाइल के साथ इस्तेमाल करें (रास्ता)
async function checkDupl() {
const arr = new Array(10).fill(0);
let code = '';
for (const i of arr) {
//generate RANDOM CODE
//for example, it would be '000001' to '000010'
code = (Math.floor(Math.random() * 10) + 1).toString().padStart(6, "0");
const params = { ... }; // it has filterExpression the code I generated randomly
const res = await DYNAMO_DB.scan(params).promise(); // convert to promise
if (res.Items.length === 0) {
/* no duplicate! */
return code;
}
return code;
}
}
(async () => {
console.log(await checkDupl());
})();
1
hoangdv
30 अक्टूबर 2019, 06:28