Search the Community
Showing results for tags 'hard'.
-
So asynchronous programming is something quite difficult to do in JavaScript, but since we now have Promises and async/await, it's becoming increasingly less complicated. Plus there is no need for "callback hell". So I will assume you understand what Promise.all() is and why it can sometimes be problematic, as it will not wait for all requests to complete and just fails on the first rejected Promise. The following function will still reject on a failed Promise, but the difference being it will wait for all the Promises to complete before resolving or rejecting. If no failure occurred, then the resolved value is the same as Promise.all(), an array of resolved values; otherwise, it returns a completed object (see below for details). Also note that the array might contain empty slots, this is so it's easier to debug which Promise failed in the array, as they're inserted in the same index slot Promise.allComplete = (iterable) => { if (!Array.isArray(iterable)) { throw new TypeError('Invalid argument, expected "iterable" to be an array'); } const completed = { resolved: [], rejected: [], }; const wrapResolutionOrRejection = (type, index) => valueOrReason => (completed[type][index] = valueOrReason); const wrappedIterable = iterable.map((value, index) => Promise.resolve(value) // The rejected wrapper function could be put in the catch, but it's wasteful for our purposes .then(wrapResolutionOrRejection('resolved', index), wrapResolutionOrRejection('rejected', index)) ) return Promise.all(wrappedIterable) .then(() => completed.rejected.length === 0 ? Promise.resolve(completed.resolved) : Promise.reject(completed)); }; // Example const requests = [ createPromise(true, 10), createPromise(false, 10), createPromise(true, 200), createPromise(true, 1000), ]; // Rejects on the first Promise which fails, but if you check in the console, it didn't wait // for the third Promise to successfully complete, as the console log came after the error log was displayed // Promise.all(requests) // .then(values => console.log('Successfully completed', values)) // .catch(err => console.error('Not successfully completed', err)) // "allComplete" is different, in that it will wait for all the Promises to be completed i.e. resolve and reject, // then resolve if all Promises were successful or reject if one Promise failed. // It returns the following data structure: // { // resolved: [...Promises which resolved, and inserted by the associated Promise's index], // rejected: [..Promises which rejected, and inserted by the associated Promise's index], // } Promise.allComplete(requests) .then(completed => console.log('Successfully completed', completed)) .catch(completed => console.log('Not successfully completed', completed)) function createPromise(isResolved, delay) { return new Promise((resolve, reject) => { setTimeout(() => { console.log(`Promise: "${delay}"`); if (isResolved) { resolve(delay); } else { reject(new Error('An unexpected error occurred')); } }, delay); }); }
-
Hello. I'm trying to create this advanced AutoIt chatbot which can learn from interacting (by words) with others. The key feature of an chatbot is the ability to learn new information. I need to know how to store unknown information, for example when a user asks "what do you know about Autoit" the bot should respond asking what is it, then the bot will read the answer from the user. Any ideas? Thanks in advance.