Category Archives: JavaScript

JS native implementation Promise.all

I met an interesting interview question today, which asked me to implement promise.all
with js native. Since I was not familiar with this API, I only realized the function of “resolve” and “callback”
Promise. All description

    promise.all (iterable) method returns a Promise instance in which all promises in the iterable parameter are “resolved” or arguments containing no Promise. If the Promise has a rejected in the parameter, the instance will call back reject because of the result of the first failed Promise

JS implements the all method

function all(iterableArr) {
    //Returns a PROMISE instance (satisfying the first rule)
    return new Promise((resolve,reject)=>{
        //resArr is used to store all the resolve promises of the resolve
        let resArr = [];
        // iterate through all elements of the array.
        for(let i in iterableArr){  
            let cur = iterableArr[i];
            // If the current object is of type promises
            if(typeof cur === 'object' && typeof cur.then ==='function'){ 
                cur.then((res)=>{
                    resArr[i] = res;
                    //If all resolve, the length of the stored resArr is the same as the length of the incoming iterableArr, and the entire promise is then resolve (in accordance with the second rule).
                    if(resArr.length === iterableArr.length){
                        resolve(resArr);
                    }
                // If the state of the current instance of promise is reject, then the entire promise will be reject. (The third rule is met.)
                },reject) 
            }else{
                resArr[i] = cur
            }
        }
    })
}

//Test
all([
    Promise.reject(100),//Promise.resolve(100)
    123,
    new Promise((resolve,reject)=>{
        setTimeout(()=>{
            resolve("DD")
        },5000)
    })
]).then((res)=>{
    console.log(res)
}).catch((err)=>{
    console.log(err)
})

The above

Using for in loop complex data types (object and array) in ES6

For in loop objects
For in looping objects, actually looping properties

   obj = {
            name: "No name",
            sex: "man",
            age: "24"
        }
   for(var i in obj) {
          console.log(i) // name sex age
      }
   for(var i in obj) {
          console.log(obj[i]) // No name man 24
      }

For in loop arrays
For in loops through an array, but it loops through the index

   arr = ["zhangsan","wangwu","lisi"]
   for(var i in arr) {
          console.log(i) //0 1 2
      }
   for(var i in arr) {
          console.log(arr[i]) //zahngsan wangwu lisi
      }

Difference between contenttype and datatype in Ajax request of jquery

The difference between contentType and dataType in jQuery AJAX requests

DataType: The type of data that you want the back end server to send to the front end. If this property is not set, what data type the server returns is a string in the appropriate type format.

//Use jQuery to write an AJAX request
function getMsg(data) {
    $.ajax({
        url:'/mobian/test1.action',
        type:'POST',
        contentType: 'application/json; charset=UTF-8', //The data sent from the front end to the backend server is in JSON format.
        async:false,
        dataType:'json', //The front end expects to receive data in JSON format.
        data:JSON.stringify(data),  //We have specified JSON format, so we need to make the data data into JSON format.
        success: function (response) {
            console.log(response);
        }
    })
}

Bugly automatically upload script and report zip error: nothing to do! In xcode10

First, the solution:
“For dsymFile in $(find “$DSYM_FOLDER” -name ‘*.dsym ‘) Add the “sleep 2s” command before the do line,
That is

            #Delay the zip operation for two seconds.
            sleep 2s

            #
            for dsymFile in $(find "$DSYM_FOLDER" -name '*.dSYM'); do
            RET="T"
            echo "Found dSYM file: $dsymFile"

The purpose was to delay the execution of the zip-r-j $DSYM_SYMBOL_ZIP_FILE $dsymfile-x *.plist command by 2 seconds.

Bugly provides the function of directly uploading dSYM files with script automatic configuration. However, when I was in XCODE10, I found that there was no corresponding dSYM file on the “Symbol Table Management” page of The Bugly website. After checking the script output at compile time of Xcode, I found the following error prompt:
zip error: Nothing to do! (…)
Error: Failed to upload the zip archive file.
FAILTURE – dSYM upload complete.
Failed to upload the dSYM
The crux of the above error is the first zip error: Nothing to do! . Because the zip did not compress successfully, *.dsym.zip could not be found to upload the symbol table.
zip error: Nothing to do! This means there is no file to compress and nothing to do.
After constant testing and speculation, it is thought that it may be the execution of the ZIP command, dSYM symbol table file is not generated, so no file can be compressed. Simply add a 2s delay command before zip, no longer report an error, and successfully upload Bugly.
But after all, this way is rude, just like adding a delay function in the iOS code without knowing when to refresh the interface, which is not reliable. But at least it solves my emergency.

Google browser plug-in JavaScript errors Notifier

JavaScript Errors Notifier is a developer tool for error notification during JavaScript debugging.

Notifies JavaScript errors by icon in toolbar bar or notification popup
GitHub → https://goo. gl/RBcFK8
Test page → https://goo.gl/IEXS2y
— FEATURES —
✔ Change extension icon color in toolbar when JavaScript error occurs
✔ Show error icon in bottom right page corner
✔ Show errors details by click on toolbar or notification icon
✔ Error source URL in popup is clickable
✔ Show errors details in notification popup
✔ Show errors stack traces
✔ Show errors column number
✔ Error source in notification popups is clickable
✔ Does not overrides user-defined error handler
✔ Handle console.error() calls
✔ Handle missing js/css/other missing files 404 errors
✔ Ignore 404 errors initiated by AdBlock and etc
✔ Ignores repeated errors
✔ Ignores Google Chrome extensions internal errors
✔ Error text is linked on StackOverflow search
✔ Copy errors details to clipboard