how to wait for async operation in javascript