promise

Run function asynchronously in Google Apps Script

空扰寡人 提交于 2020-07-22 06:01:44
问题 I'm making a Slack bot that calls a GAS function. Everything is working except Slack shows an error message because it only waits 3 seconds for a response when calling an API. Can anyone help me to work out how to run everyDay2 asynchronously so that I can return the response before it's finished. I've tried Promises and callbacks but I can't solve it. function doPost(e){ const promise = new Promise(everyDay2); return ContentService.createTextOutput('thinking...'); } 回答1: Promises doesn't

Angular Observable need to complete first

房东的猫 提交于 2020-07-22 03:24:26
问题 @Input() list: string[]; ngOnInit() : void { this.valueMap = new Map<any, any>(); this.getDataFromService(); this.buildContainer(); } private getDataFromService(): void { this.list.forEach(value-> { this.fetchService(value).subscribe(data ->{ this.valueMap.set(value,data); } ) }) } private buildContainer(): void { console.log(this.valueMap.size); // shows always 0 even when service returns the data } Now thing is that I have to use this valueMap in the method buidlContainer() , hence I need

Angular Observable need to complete first

喜你入骨 提交于 2020-07-22 03:23:16
问题 @Input() list: string[]; ngOnInit() : void { this.valueMap = new Map<any, any>(); this.getDataFromService(); this.buildContainer(); } private getDataFromService(): void { this.list.forEach(value-> { this.fetchService(value).subscribe(data ->{ this.valueMap.set(value,data); } ) }) } private buildContainer(): void { console.log(this.valueMap.size); // shows always 0 even when service returns the data } Now thing is that I have to use this valueMap in the method buidlContainer() , hence I need

nodejs run async function one after another

泪湿孤枕 提交于 2020-07-21 06:24:58
问题 I'm new to JS/nodejs, so please pardon me if I can't ask to-the-point question. So basically, if I have two async functions, async function init() {...} async function main() {...} How can I make sure to call main() after init() has finished its async requests? Specifically, I want to make use of the module https://www.npmjs.com/package/hot-import whereas on its page, there is a sample code: async function main() { const MODULE_CODE_42 = 'module.exports = () => 42' const MODULE_CODE_17 =

How to use Async Wait with HTML5 GeoLocation API?

一曲冷凌霜 提交于 2020-07-20 17:15:29
问题 The first method returns promise. getCoordinates() { return new Promise(function(resolve, reject) { navigator.geolocation.getCurrentPosition(resolve, reject); }); } Returns the result of reverseGeoCode method. async getAddress() { await this.getCoordinates().then(position => { let latitude = position.coords.latitude; let longitude = position.coords.longitude; let url = Constants.OSMAP_URL + latitude + "&lon=" + longitude; // Reverse geocoding using OpenStreetMap return this.reverseGeoCode(url

How to understand this Promise execution order?

耗尽温柔 提交于 2020-07-19 01:48:56
问题 I don’t understand why this piece of code results in such an order? Could anyone elaborate on this? I thought Promises were like a FIFO queue, but the nested Promise functions seems a little bit unpredictable, or maybe using some other data structure? new Promise(resolve => { resolve() }) .then(() => { new Promise(resolve => { resolve() }) .then(() => { console.log(1) }) .then(() => { console.log(2) }) .then(() => { console.log(3.1) }) }) .then(() => { console.log(1.1) new Promise((resolve =>

How to understand this Promise execution order?

我的未来我决定 提交于 2020-07-19 01:47:25
问题 I don’t understand why this piece of code results in such an order? Could anyone elaborate on this? I thought Promises were like a FIFO queue, but the nested Promise functions seems a little bit unpredictable, or maybe using some other data structure? new Promise(resolve => { resolve() }) .then(() => { new Promise(resolve => { resolve() }) .then(() => { console.log(1) }) .then(() => { console.log(2) }) .then(() => { console.log(3.1) }) }) .then(() => { console.log(1.1) new Promise((resolve =>

How to understand this Promise execution order?

时间秒杀一切 提交于 2020-07-19 01:45:23
问题 I don’t understand why this piece of code results in such an order? Could anyone elaborate on this? I thought Promises were like a FIFO queue, but the nested Promise functions seems a little bit unpredictable, or maybe using some other data structure? new Promise(resolve => { resolve() }) .then(() => { new Promise(resolve => { resolve() }) .then(() => { console.log(1) }) .then(() => { console.log(2) }) .then(() => { console.log(3.1) }) }) .then(() => { console.log(1.1) new Promise((resolve =>

Promises/Fetch in JavaScript: how to extract text from text file

走远了吗. 提交于 2020-07-18 10:59:05
问题 I'm working on a small program in JavaScript. Basically, I want to use Promise and fetch to extract text out of two text files. However, I can't figure out how to get the actual text out of the files. Here's my current code. sample.txt this is a sample text file. sample2.txt this is the second sample file. index.js function getSampleText() { Promise.all([ fetch('sample.txt'), fetch('sample2.txt') ]).then(allResp => { let sampleResp = allResp[0]; let sample2Resp = allResp[1]; console.log

Working of call stack when async/await is used

走远了吗. 提交于 2020-07-18 07:37:20
问题 How does the Call Stack behave when async/await functions are used ? const asyncFuntion=async()=>{ //some asynchronous code } const first = async()=>{ await asyncFuntion(); console.log('first completed'); debugger; } const second = ()=>{ console.log('second completed'); debugger; } function main(){ first(); second(); } main(); In the above code, when the first breakpoint is encountered in second(), I could see that the call stack contained main() and second(). And during the second breakpoint