forked from MapComplete/MapComplete
		
	Add robustness to download script
This commit is contained in:
		
							parent
							
								
									7101baf13b
								
							
						
					
					
						commit
						703b66195f
					
				
					 1 changed files with 28 additions and 22 deletions
				
			
		| 
						 | 
				
			
			@ -17,37 +17,43 @@ export default class ScriptUtils {
 | 
			
		|||
        }
 | 
			
		||||
        return result;
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    public static DownloadJSON(url) : Promise<any>{
 | 
			
		||||
        return new Promise((resolve, reject) => {
 | 
			
		||||
            https.get(url, (res) => {
 | 
			
		||||
                const parts : string[] = []
 | 
			
		||||
                res.setEncoding('utf8');
 | 
			
		||||
                res.on('data', function (chunk) {
 | 
			
		||||
                    // @ts-ignore
 | 
			
		||||
                    parts.push(chunk)
 | 
			
		||||
                });
 | 
			
		||||
 | 
			
		||||
                res.addListener('end', function () {
 | 
			
		||||
                    const result = parts.join("")
 | 
			
		||||
                    try{
 | 
			
		||||
                        resolve(JSON.parse(result))
 | 
			
		||||
                    }catch (e){
 | 
			
		||||
                        reject(e)
 | 
			
		||||
                    }
 | 
			
		||||
                });
 | 
			
		||||
            })
 | 
			
		||||
    public static DownloadJSON(url): Promise<any> {
 | 
			
		||||
        return new Promise((resolve, reject) => {
 | 
			
		||||
            try {
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
                https.get(url, (res) => {
 | 
			
		||||
                    const parts: string[] = []
 | 
			
		||||
                    res.setEncoding('utf8');
 | 
			
		||||
                    res.on('data', function (chunk) {
 | 
			
		||||
                        // @ts-ignore
 | 
			
		||||
                        parts.push(chunk)
 | 
			
		||||
                    });
 | 
			
		||||
 | 
			
		||||
                    res.addListener('end', function () {
 | 
			
		||||
                        const result = parts.join("")
 | 
			
		||||
                        try {
 | 
			
		||||
                            resolve(JSON.parse(result))
 | 
			
		||||
                        } catch (e) {
 | 
			
		||||
                            reject(e)
 | 
			
		||||
                        }
 | 
			
		||||
                    });
 | 
			
		||||
                })
 | 
			
		||||
            } catch (e) {
 | 
			
		||||
                reject(e)
 | 
			
		||||
            }
 | 
			
		||||
        })
 | 
			
		||||
       
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public static sleep(ms) {
 | 
			
		||||
        if(ms <= 0){
 | 
			
		||||
        if (ms <= 0) {
 | 
			
		||||
            process.stdout.write("\r                                       \r")
 | 
			
		||||
            return;
 | 
			
		||||
        }
 | 
			
		||||
        return new Promise((resolve) => {
 | 
			
		||||
            process.stdout.write("\r Sleeping for "+(ms/1000)+"s \r")
 | 
			
		||||
            process.stdout.write("\r Sleeping for " + (ms / 1000) + "s \r")
 | 
			
		||||
            setTimeout(resolve, 1000);
 | 
			
		||||
        }).then(() => ScriptUtils.sleep(ms - 1000));
 | 
			
		||||
    }
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue