|
| 1 | +const fs = require('fs'); |
| 2 | +const path = require('path'); |
| 3 | +const forPromise = require('../dist/index.cjs'); |
| 4 | + |
| 5 | +const tiny_test = async function () { |
| 6 | + // The Test |
| 7 | + |
| 8 | + // Number |
| 9 | + await forPromise({ data: 3 }, function (item, fn) { |
| 10 | + // Test Value |
| 11 | + console.log(`Number: ${item}`); |
| 12 | + |
| 13 | + // Complete |
| 14 | + fn(); |
| 15 | + }); |
| 16 | + |
| 17 | + // Object |
| 18 | + await forPromise( |
| 19 | + { |
| 20 | + data: { |
| 21 | + test1: true, |
| 22 | + test2: true, |
| 23 | + }, |
| 24 | + }, |
| 25 | + function (item, fn) { |
| 26 | + // Test Value |
| 27 | + console.log(`Object: ${item}`); |
| 28 | + |
| 29 | + // Complete |
| 30 | + fn(); |
| 31 | + }, |
| 32 | + ); |
| 33 | + |
| 34 | + // Array |
| 35 | + await forPromise( |
| 36 | + { |
| 37 | + data: [1, 2, 3], |
| 38 | + }, |
| 39 | + function (item, fn) { |
| 40 | + // Test Value |
| 41 | + console.log(`Array: ${item}`); |
| 42 | + |
| 43 | + // Complete |
| 44 | + fn(); |
| 45 | + }, |
| 46 | + ); |
| 47 | + |
| 48 | + // While |
| 49 | + const whileData = { count: 0 }; |
| 50 | + await forPromise( |
| 51 | + { |
| 52 | + type: 'while', |
| 53 | + while: whileData, |
| 54 | + checker: function () { |
| 55 | + return whileData.count < 3; |
| 56 | + }, |
| 57 | + }, |
| 58 | + function (fn) { |
| 59 | + // Test Value |
| 60 | + console.log(`Do: ${whileData.count}`); |
| 61 | + |
| 62 | + // Count |
| 63 | + whileData.count++; |
| 64 | + |
| 65 | + fn(); |
| 66 | + }, |
| 67 | + ); |
| 68 | + |
| 69 | + // The Data |
| 70 | + const data = [1, 2]; |
| 71 | + const data2 = [1, 2]; |
| 72 | + |
| 73 | + // Start For Script |
| 74 | + await forPromise({ data: data }, function (index, fn, fn_error, extra) { |
| 75 | + // Show Index |
| 76 | + console.group(`For (Normal): '${index}'`); |
| 77 | + |
| 78 | + // Add Extra For Script for the "data2" |
| 79 | + const extraForAwait = extra({ data: data2 }); |
| 80 | + |
| 81 | + // Execute the extra For Script |
| 82 | + extraForAwait.run(function (index2, fn2) { |
| 83 | + // Show Index |
| 84 | + console.log(`For (Extra): '${index2}'`); |
| 85 | + fn2(); |
| 86 | + }); |
| 87 | + |
| 88 | + console.groupEnd(); |
| 89 | + |
| 90 | + // Complete Here |
| 91 | + fn(); |
| 92 | + }); |
| 93 | + |
| 94 | + // Force Break |
| 95 | + await forPromise( |
| 96 | + { |
| 97 | + data: [1, 2, 3], |
| 98 | + }, |
| 99 | + function (item, fn) { |
| 100 | + // Test Value |
| 101 | + console.log(`Array with Force Break: ${item}`); |
| 102 | + |
| 103 | + // Force Complete |
| 104 | + fn(true); |
| 105 | + }, |
| 106 | + ); |
| 107 | + |
| 108 | + await forPromise( |
| 109 | + { |
| 110 | + data: [1, 2, 3], |
| 111 | + }, |
| 112 | + function (item, fn, fn_error) { |
| 113 | + // Wait Script |
| 114 | + fs.readdir(path.join(__dirname, '../src'), (err, files) => { |
| 115 | + // Success! The "fn()" will say that the execution of this script has ended. |
| 116 | + if (!err) { |
| 117 | + console.log(`Force Break used to read this data: ${item}`); |
| 118 | + console.log(files); |
| 119 | + fn({ forceResult: true }); |
| 120 | + } |
| 121 | + |
| 122 | + // Error! The execution of the promise will be interrupted here! |
| 123 | + else { |
| 124 | + fn_error(err); |
| 125 | + } |
| 126 | + }); |
| 127 | + }, |
| 128 | + ); |
| 129 | + |
| 130 | + // Complete |
| 131 | + console.log('Complete!'); |
| 132 | + return; |
| 133 | +}; |
| 134 | + |
| 135 | +// Start the Test |
| 136 | +tiny_test(); |
0 commit comments