How to avoid queuing delays when using async/await?
for (var i = 0; i < 3; i++) {
await SomeFunction(i)
}
// 1, 2, 3
This queing driving me nuts especially when working with different APIs, but I don't want to give up on using super convinient await and returns to this:
for (var i = 0; i < 3; i++) {
SomeFunction(i)
.then(i => {
})
}
// 2, 1, 3
You can put all the Promises into an array, and then await them all at once with Promise.all:
var promises = [];
for (var i = 0; i < 3; i++) {
promises.push(SomeFunction(i));
}
await Promise.all(promises);
Related
I'm trying to pick a random color for strokeline from an array list. From the program, it indicates that random() expect parameter like random(float). But I feel a bit stuck of how to correct it. Below is my code:
int n = 200;
drop[] d;
color[] colors = {
#013a63,
#01497c,
#014f86,
#2a6f97,
#2c7da0,
#468faf,
#61a5c2,
#89c2d9,
#a9d6e5,
};
void setup() {
size(1080, 720);
stroke(random(colors));
d = new drop[n];
for(int i = 0; i < d.length; i ++) {
d[i] = new drop(random(width), random(-10, 20), random(5));
}
}
void draw() {
background(1, 42, 74,10);
for(int i = 0; i < d.length; i ++) {
d[i].show();
d[i].update();
}
}
Generate a random # between 0 and 8, then use that index:
stroke(colors[(int)random(8)]);
or dynamically using the length or colors (in case it changes):
stroke(colors[(int)random(colors.length)]);
Unable to run this code, tried using if else statement inside for loop. Want to skip q6:q10 data. Please help. Using it in Google Appscript for google spreadsheet
{ var data1=[];;
for(var i=0; i<dataLen; i++)
for (q = 0;q<20;if (q=5, q+=4; else q++)
{
data1[q]=data[i][q];
}
ss.appendRow(data1);
flag="true";
The format and lack of context makes this question very confusing, but a loop like this solves your stated problem
var data1=[];
for(var i=0; i<dataLen; i++){
for(q = 0; q < 20;q++){
if(q == 7){
q = 10;
}
data1[q] = data[i][q];
}
}
If you would like to use 2 for loops, it would look like
var data1=[];
for(var i=0; i<dataLen; i++){
for(q = 0; q < 7;q++){
data1[q] = data[i][q];
}
}
for(var i=0; i<dataLen; i++){
for(q = 10; q < 20;q++){
data1[q] = data[i][q];
}
}
I don't know what you really want, but I think you want something like this :
For(var i = 0; i<dataLen; i++){
if(i%10 == 6||i%10 == 7||i%10 == 8||i%10 == 9||i%10 == 0){
} else {
data1[i]=data[q][i];
ss.appendRow(data1);
flag = "true";
}
}
var selectionSort = function(array) {
var minIndex;
for(var i = 0;i <array.length;i++){
minIndex = indexOfMinimum(array,i);}
swap(array,minIndex,i);
};
where indexOfMinimum is used to find the index of min value for the subarray starting at index i. And swap is a popularly known function.
You've got a problem with curly braces, see minIndex = indexOfMinimum(array,i);}. So basically swap(array,minIndex,i); is executed only once, not in the loop body.
Your code with corrected style:
var selectionSort = function(array) {
var minIndex;
for(var i = 0; i < array.length; i++) {
minIndex = indexOfMinimum(array,i);
}
swap(array,minIndex,i);
};
What you need:
var selectionSort = function(array) {
var minIndex;
for(var i = 0; i < array.length; i++){
minIndex = indexOfMinimum(array,i);
swap(array,minIndex,i);
}
};
for (int i = 0; i < 1000; i++)
{
PrivateMessage privateMessage = new PrivateMessage()
{
id=i
};
dc.PrivateMessages.InsertOnSubmit(privateMessage);
dc.SubmitChanges();
}
This method is suitable for insert 1000 records?
There are better ways to do this?
Yeah-- probably best not to do your InsertOnSubmit in a loop. Try:
List<PrivateMessage> messages = new List<PrivateMessage>();
for (int i = 0; i < 1000; i++)
{
messages.Add(new PrivateMessage() { id=i });
}
dc.PrivateMessages.InsertAllOnSubmit(messages);
dc.SubmitChanges();
have dc.SubmitChanges() out of loop.
for (int i = 0; i < 1000; i++)
{
PrivateMessage privateMessage = new PrivateMessage()
{
id=i
};
dc.PrivateMessages.InsertOnSubmit(privateMessage);
}
dc.SubmitChanges();
Here's another one:
Enumerable.Range(1, 1000).ToList().ForEach(x =>
dc.PrivateMessages.InsertOnSubmit(new PrivateMessage(){id=x}));
dc.SubmitChanges();
Below is some core graphics code..
CGColorRef colorRefArray[MAGIC_NUM];
for (int i = 0; i < MAGIC_NUM ; i++)
{
...
colorRefArray[i] = CreateColor(colorValueForEachColor, numberofcomp);
}
colorRefArray already has memory and CreateColor(); will again create a memory and it leads to memory leak.
How do I avoid this situation?
One possible thought I have is
CGColorRef colorRefArray[MAGIC_NUM];
for (int i = 0; i < MAGIC_NUM ; i++)
{
...
CGColorRef colorref = CreateColor(colorValueForEachColor, numberofcomp);
colorRefArray[i] = colorref;
CFRelease(colorref);
}
Is this approach correct?
No it's not. You're immediately releasing the color you created. The correct approach would be this:
CGColorRef colorRefArray[MAGIC_NUM];
for (int i = 0; i < MAGIC_NUM ; i++)
{
...
colorRefArray[i] = CreateColor(colorValueForEachColor, numberofcomp);
}
//Use your colors
//Now release them
for (int i = 0; i < MAGIC_NUM ; i++)
{
CFRelease(colorRefArray[i]);
}
No, because then colorRefArray will be filled with invalid pointers.
Try using a CFMutableArray instead of a raw C array. Then you only have to worry about the reference to the array, as it will own the colours for you:
CFArrayRef CopyColorArray(void) {
CFMutableArrayRef colorRefArray = CFArrayCreateMutable(NULL, 0, &kCFTypeArrayCallBacks);
if (colorRefArray) {
for (int i = 0; i < MAGIC_NUM ; i++) {
...
CGColorRef colorref = CreateColor(colorValueForEachColor, numberofcomp);
if (colorref) {
CFArrayAppendValue(colorRefArray, colorref);
CFRelease(colorref);
}
}
}
return colorRefArray;
}