Q: Best way to access aync function returns I've the following code in a react application. I cannot access the return values in a separate files since this is an async task. What is the best approach to catch the returned values from this function? Is it to wrap the code with a promise object? 
import jsYaml from 'js-yaml';
import SwaggerValidator from 'swagger-parser';

export function parseYaml(yamlString) {
    let parsedYamlJson;
    try {
        parsedYamlJson = jsYaml.safeLoad(yamlString);
        SwaggerValidator.validate(parsedYamlJson)
        .then((parsedJson)=>{
            return { 
                isError: false,
                message: '',
                swaggerJson: parsedJson
            }
        })
        .catch((parseException)=>{
            return { 
                isError: true,
                message: 'Invalid Swagger JSON',
                swaggerJson: {}
            }
        })
    } catch (yamlException) {
        return { 
            isError: true,
            message: yamlException.message,
            swaggerJson: {}
        }
    }
}


A: You could use a Promise:
export function parseYaml(yamlString) {
    return new Promise((resolve, reject) => {
    let parsedYamlJson;
    try {
        parsedYamlJson = jsYaml.safeLoad(yamlString);
        SwaggerValidator.validate(parsedYamlJson)
        .then((parsedJson)=>{
            resolve({ 
                isError: false,
                message: '',
                swaggerJson: parsedJson
            })
        })
        .catch((parseException)=>{
            reject({ 
                isError: true,
                message: 'Invalid Swagger JSON',
                swaggerJson: {}
            })
        })
    } catch (yamlException) {
        reject({ 
            isError: true,
            message: yamlException.message,
            swaggerJson: {}
        })
    }
    })
}

And then in other file:
parseYaml('someString').then(res => {
    console.log(res)
}).catch(err => {
    console.log('error:', err)
})

