Saturday, October 15, 2022

How Promise.all works

Promise.all waits till all the promises passed to it are resolved. Here is an example code snippet:


async function fun1() {
console.log("1 before wait");
return new Promise((resolve, reject) => {
setTimeout(() => {
console.log("1 after wait");
resolve();
}, 3000);
});
}

async function fun2() {
console.log("2 before wait");
return new Promise((resolve, reject) => {
return setTimeout(() => {
console.log("2 after wait");
resolve();
}, 2000);
});
}

async function main() {
console.log("start");
await Promise.all([fun1(), fun2()]);
console.log("end");
}
main();

The above snippet outputs
start
1 before wait
2 before wait
2 after wait
1 after wait
end

Tuesday, May 4, 2021

Running cordova with supervisor

 supervisor helps you automatically restart your process when something in your directory, or any type of files changes.  This is particularly helped for developers making changes to their code base. They need not manually restart the server for each change. supervisor can take care of restarting.

To install supervisor globally, use:

# sudo npm install supervisor -g

When running a cordova server locally for testing, in order to use supervisor, use the following:

$ supervisor --ignore platforms -- /usr/local/bin/cordova run <platform>

For example:

$ supervisor --ignore platforms -- /usr/local/bin/cordova run browser

The --ignore platforms is required, else, during the build, the code gets changed in the platforms folder, and supervisor will unnecessarily restart the process.

Friday, April 23, 2021

Running a local server, but using the production domain

Say you are building www.mycooldomain.com and want to test your changes locally, but using the www.mycooldomain.com domain itself in your browser.


Option 1: Use /etc/hosts file (works on both linux and mac machines)

Add an entry to your /etc/hosts file, like below:

127.0.0.1    www.mycooldomain.com


This option works as long as you do not want https to work. However, if you want https also to work, go for option 2.


Option 2: Using Charles proxy on a mac

Step 1: Install Charles Proxy, and add a setting under Tools -> Map Remote like the one below:



Step 2: Then, in the Charles proxy app, go to Help -> SSL Proxying -> Install Charles Root Certificate.  Once the certificate is installed, in the Keychain Access app, double click on the Charles Proxy CA certificate, click on Trust, and choose "Always Trust" for "When using this certificate".

Now you are all set to test with your prod domain, running locally.

Monday, April 6, 2020

Cordova plugin for ios

A good post on writing a cordova plugin for ios: https://medium.com/@JordanBenge/how-to-write-an-ionic-cordova-plugin-in-swift-8d443430b27d.   Found this particularly useful when migrating a former phonegap app that was written in an older version of cordova to latest version wherein the ios code is in Swift 4.  For functions written in older version of swift no annotation was required.  However, for cordova integration to work with the Swift 4 migration, had to add the @objc annotation to the class and functions in Swift.

Sunday, May 22, 2016

'init' of undefined when using gapi with angular

This can be solved by calling gapi.auth2.init in window.gapi_onload and loading google api js through javascript, as follows:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
export class AppComponent implements OnInit {
    ngOnInit() {
        const gappClientId = "xxxxxx.apps.googleusercontent.com";

        window.gapi_onload = function () {
            console.log("gapi onload called");
            var auth2 = gapi.auth2.init({
                client_id: gappClientId,
                scope: 'profile'
            });
        };
        var po = document.createElement('script');
        po.type = 'text/javascript';
        po.async = true;
        po.src = 'https://apis.google.com/js/auth2.js';

        var s = document.getElementsByTagName('script')[0];
        s.parentNode.insertBefore(po, s);

    }
}

Sunday, May 1, 2016

In place swap in Javascript

You can do a in-place swap using a buffer in JS with the following code. Strings are themselves immutable in JS. Hence, you cannot do a in-place swap with Strings in JS.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
function reverseString(str) {
    var temp;
    for (var i = 0; i<str.length/2; i++) {
        temp = str[i];
        str[i] = str[str.length - i - 1];
        str[str.length - i - 1] = temp;
    }
}

var a = new Buffer("how are you");
console.log(a.toString());
reverseString(a);
The above code outputs:

1
2
how are you
uoy era woh

Monday, November 30, 2015

npm install taking a lot of time

Workaround:

Use http to connect to the npm registry as follows:

npm config set registry http://registry.npmjs.org/

Fix the dependencies to a version for dependencies present in package.json

npm install --save helps you to install/reinstall the dependencies and put their versions in package.json.

However, what if the packages are already installed with *s for the version in package.json and you want to be more specific about the versions in the package.json.  In that case, instead of using npm install --save and reinstall the packages, you can use this node script:

var packageJson, sys, exec, async, depJson, fs, deps;

sys = require('sys');
exec = require('child_process').exec;
async = require('async');
fs = require('fs');

packageJson = require(process.env.PWD + "/package.json");

deps = Object.keys(packageJson.dependencies);
deps = deps.concat(Object.keys(packageJson.devDependencies));

async.each(deps,
    function (dep, cb) {
        console.log("Executing : npm ls --json " + dep);
        exec("npm ls --json " + dep, function (error, stdout) {
            var versionJson = JSON.parse(stdout);
            if (packageJson.dependencies[dep]) {
                packageJson.dependencies[dep] = "~" + versionJson.dependencies[dep].version;
            }
            if (packageJson.devDependencies[dep]) {
                packageJson.devDependencies[dep] = "~" + versionJson.dependencies[dep].version;
            }
            console.log(dep + ": " + versionJson.dependencies[dep].version);
            cb();
        });
    },
    function (err) {
        if (err) {
            console.error("Following error occurred; not modifying package.json");
            console.error(err);
            return;
        }
        fs.writeFile(process.env.PWD + "/package.json", JSON.stringify(packageJson, null, 2));
    });

Thursday, October 22, 2015

ul without bullets

Here is how you can get a unordered list without bullets:

ul {
    list-style-type: none;
    padding: 0;
}

Friday, March 28, 2014

Copying the value of input field in one form to another form

This snippet copies the value of input field #source to another form #hiddenForm.
function copyFieldToForm(source, hiddenForm) {
    var clone = source.clone();
    source.after(clone).appendTo(hiddenForm);
}

copyFieldToForm($("#source"), $("#hiddenForm"));