Skip to content

In magento 2 javascript , How i can wait till a component/node initialize then i can excute my code?

How to wait till components exists in registry . I want to wait till global component “messages” initialize then I can add error/success message.

// view/frontend/templates/messages.phtml
<script type="text/x-magento-init">
    {
        "*": {
            "Magento_Ui/js/core/app": {
                "components": {
                        "messages": {
                            "component": "Magento_Theme/js/view/messages"
                        }
                    }
                }
            }
    }
</script>

I have try ‘Magento_Ui/js/lib/view/utils/async’ but it’s seam don’t work. It does not run in debugger 1

define([
    'jquery',
    'Magento_Customer/js/customer-data',
    'Magento_Ui/js/lib/view/utils/async'
], function ($, customer_data) {
    $.async('> *', 'messages', function(node){
        // debugger 1;
        customer_data.set('messages',{'test':'your cookie have expires, you have to login again','type':'success'});
    })
}

How I can solve this problem.