javascript make asynchronous function synchronous